Skip to content

Commit

Permalink
sample
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-garcia committed May 28, 2021
1 parent 6238c4d commit 5a3c371
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 7 deletions.
7 changes: 7 additions & 0 deletions Sentry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.EntityFramework", "s
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.EntityFramework.Tests", "test\Sentry.EntityFramework.Tests\Sentry.EntityFramework.Tests.csproj", "{840B220E-68EC-4ECB-AEA1-67B0151F17FC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Samples.EntityFramework", "samples\Sentry.Samples.EntityFramework\Sentry.Samples.EntityFramework.csproj", "{8E4BA4C7-413C-4668-8F41-32F484FFB7AA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -265,6 +267,10 @@ Global
{840B220E-68EC-4ECB-AEA1-67B0151F17FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{840B220E-68EC-4ECB-AEA1-67B0151F17FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{840B220E-68EC-4ECB-AEA1-67B0151F17FC}.Release|Any CPU.Build.0 = Release|Any CPU
{8E4BA4C7-413C-4668-8F41-32F484FFB7AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8E4BA4C7-413C-4668-8F41-32F484FFB7AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E4BA4C7-413C-4668-8F41-32F484FFB7AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E4BA4C7-413C-4668-8F41-32F484FFB7AA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -307,6 +313,7 @@ Global
{274CEDC2-2129-469D-B269-649EFA2EF5E0} = {77454495-55EE-4B40-A089-71B9E8F82E89}
{8B38F62E-0DD5-486F-96F5-2025AFB9B491} = {AF6AF4C7-8AA2-4D59-8064-2D79560904EB}
{840B220E-68EC-4ECB-AEA1-67B0151F17FC} = {83263231-1A2A-4733-B759-EEFF14E8C5D5}
{8E4BA4C7-413C-4668-8F41-32F484FFB7AA} = {77454495-55EE-4B40-A089-71B9E8F82E89}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0C652B1A-DF72-4EE5-A98B-194FE2C054F6}
Expand Down
43 changes: 43 additions & 0 deletions samples/Sentry.Samples.EntityFramework/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.Data.Common;
using System.Data.Entity;
using Sentry;
using Sentry.EntityFramework;

// Add this as early as possible to collect breadcrumbs:
SentryDatabaseLogging.UseBreadcrumbs();

var _ = SentrySdk.Init(o =>
{
o.Debug = true; // To see SDK logs on the console
o.Dsn = "https://eb18e953812b41c3aeb042e666fd3b5c@o447951.ingest.sentry.io/5428537";
// Add the EntityFramework integration to the SentryOptions of your app startup code:
o.AddEntityFramework();
});

var dbConnection = Effort.DbConnectionFactory.CreateTransient();
using var db = new SampleDbContext(dbConnection, true);

var user = new SampleUser();
db.Users.Add(user);

// This will throw a DbEntityValidationException and crash the app
// But Sentry will capture the error.
db.SaveChanges();

public class SampleUser
{
[Key]
public int Id { get; set; }
[Required]
public string RequiredColumn { get; set; }
}

public class SampleDbContext : DbContext
{
public DbSet<SampleUser> Users { get; set; }
public SampleDbContext(DbConnection connection, bool ownsConnection)
: base(connection, ownsConnection)
{ }
}
10 changes: 10 additions & 0 deletions samples/Sentry.Samples.EntityFramework/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Entity Framework 6 sample

If you're using Entity Framework Core, you don't need the package used in this sample.
Sentry can integrate with EF Core through `Sentry.Extensions.Logging` already.
Sentry.EntityFramework` is used to integrated with EF 6 only.

![Crash in Sentry](crash.png)

Breadcrumbs for different EF core events:
![Example in Sentry](ef.png)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Effort.EF6" Version="2.2.13" />
<ProjectReference Include="../../src/Sentry.EntityFramework/Sentry.EntityFramework.csproj" />
<ProjectReference Include="../../src/Sentry/Sentry.csproj" />
</ItemGroup>

</Project>
Binary file added samples/Sentry.Samples.EntityFramework/crash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
11 changes: 9 additions & 2 deletions src/Sentry.EntityFramework/Sentry.EntityFramework.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net461</TargetFrameworks>
<TargetFrameworks>net461;netstandard2.1</TargetFrameworks>
<PackageId>Sentry.EntityFramework</PackageId>
<AssemblyName>Sentry.EntityFramework</AssemblyName>
<RootNamespace>Sentry.EntityFramework</RootNamespace>
Expand All @@ -10,8 +10,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EntityFramework" Version="6.0.0" />
<ProjectReference Include="..\Sentry\Sentry.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net461'">
<PackageReference Include="EntityFramework" Version="6.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="EntityFramework" Version="6.3.0" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions src/Sentry.EntityFramework/SentryDatabaseLogging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public static class SentryDatabaseLogging
/// This is a static setup call, so make sure you only call it once for each <see cref="IQueryLogger"/> instance you want to register globally
/// </summary>
/// <param name="logger"></param>
public static SentryCommandInterceptor UseBreadcrumbs(IQueryLogger logger = null)
public static SentryCommandInterceptor UseBreadcrumbs(IQueryLogger? logger = null)
{
logger = logger ?? new SentryQueryLogger();
logger ??= new SentryQueryLogger();
var interceptor = new SentryCommandInterceptor(logger);
DbInterception.Add(interceptor);
return interceptor;
Expand Down
2 changes: 1 addition & 1 deletion src/Sentry.EntityFramework/SentryQueryLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ internal class SentryQueryLogger : IQueryLogger
{
private readonly IHub _hub;

public SentryQueryLogger(IHub hub = null) => _hub = hub ?? HubAdapter.Instance;
public SentryQueryLogger(IHub? hub = null) => _hub = hub ?? HubAdapter.Instance;

public void Log(string text, BreadcrumbLevel level = BreadcrumbLevel.Debug)
=> _hub.AddBreadcrumb(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net461</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<!-- Running these tests on Mono fail -->
<PropertyGroup Condition="'$(OS)' == 'Windows_NT'">
<TargetFrameworks>net461;$(TargetFrameworks)</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Effort.EF6" Version="1.3.5" />
<PackageReference Include="Effort.EF6" Version="2.2.13" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 5a3c371

Please sign in to comment.