Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sentry.EntityFramework monorepo #1017

Merged
merged 38 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
30823d9
Create README.md
bruno-garcia Jul 16, 2018
3a67bfa
Move EF integration from sentry-dotnet to its own repository
kanadaj Jul 16, 2018
8ad0792
Merge pull request getsentry/sentry-dotnet-ef#1 from kanadaj/master
bruno-garcia Jul 16, 2018
eafa4fa
Fix dependencies, still doesn't compile still NuGet isn't up-to-date …
kanadaj Jul 16, 2018
3baedc8
Remove dependency on Sentry.Testing
kanadaj Jul 16, 2018
bfd88d2
Merge pull request getsentry/sentry-dotnet-ef#2 from kanadaj/master
bruno-garcia Jul 16, 2018
e332b94
Update package reference to new NuGet package for required dependencies
kanadaj Jul 17, 2018
bfad730
Merge pull request getsentry/sentry-dotnet-ef#3 from kanadaj/master
bruno-garcia Jul 17, 2018
434f56f
Project structure
bruno-garcia Jul 19, 2018
af7fd94
Build
bruno-garcia Jul 19, 2018
dd90fbc
Split restore and build
bruno-garcia Jul 19, 2018
d19c7bc
Appveyor restores nuget packges
bruno-garcia Jul 19, 2018
eb3b067
Appveyor artifact only from src
bruno-garcia Jul 19, 2018
ce45795
Add appveyor badge
bruno-garcia Jul 19, 2018
dcfe498
Make integrations public, bump to preview2
bruno-garcia Jul 30, 2018
70eb1c4
Fix appveyor badge
bruno-garcia Jul 30, 2018
9a494c9
fix: branch name on appveyor badge
bruno-garcia Aug 16, 2018
2b31b6b
ref: Set project URL to sentry.io
bruno-garcia Aug 27, 2018
b03cc7b
ref: format internalsVisibleTo to avoid iOS issue
bruno-garcia Jan 4, 2019
e817ff3
dep: Latest Sentry SDK
bruno-garcia Jan 4, 2019
3b8ddea
rel: release 1.0.0
bruno-garcia Jan 4, 2019
2853f48
ref: don't patch csproj
bruno-garcia Jan 4, 2019
9e17be2
Updated Sentry version to 2.1.6
co7e Oct 23, 2020
4bb88e3
Merge pull request getsentry/sentry-dotnet-ef#9 from co7e/master
bruno-garcia Oct 23, 2020
1bbd744
bump 2.1.6
bruno-garcia Oct 23, 2020
880048d
2.1.6
bruno-garcia Oct 23, 2020
e86e1f4
Bump Sentry SDK version to 3.0
lucas-zimerman Feb 6, 2021
b1a2741
Fix test issues.
lucas-zimerman Feb 6, 2021
f92ebf2
Merge pull request getsentry/sentry-dotnet-ef#12 from lucas-zimerman/…
bruno-garcia Feb 6, 2021
25a7450
Update README.md
bruno-garcia Mar 10, 2021
7659ddf
csproj version 3.0.3
bruno-garcia May 28, 2021
594b43b
monorepo: Sentry.EntityFramework
bruno-garcia May 28, 2021
6238c4d
remove sample
bruno-garcia May 28, 2021
a466fdd
sample
bruno-garcia May 28, 2021
d170ae1
make public DiagnosticLoggerExtensions
bruno-garcia May 28, 2021
e366b94
ef6 refactor
bruno-garcia May 28, 2021
f089dd5
fix tfm
bruno-garcia May 28, 2021
4db8360
changelog and diag log fix
bruno-garcia May 28, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .craft.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ targets:
type: sdk
config:
canonical: "nuget:Sentry.AspNet"
- name: registry
type: sdk
config:
canonical: "nuget:Sentry.EntityFramework"
- name: registry
type: sdk
config:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

### Features

- Sentry.EntityFramework moved to this repository ([#1017](https://github.com/getsentry/sentry-dotnet/pull/1017))
- Additional `netstandard2.1` target added. Sample with .NET Core 3.1 console app.
- `UseBreadcrumbs` is called automatically by `AddEntityFramework`

### Fixes

- normalize line breaks ([#1016](https://github.com/getsentry/sentry-dotnet/pull/1016))
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Sentry SDK for .NET
| **Sentry.AspNetCore** | [![Downloads](https://img.shields.io/nuget/dt/Sentry.AspNetCore.svg)](https://www.nuget.org/packages/Sentry.AspNetCore) | [![NuGet](https://img.shields.io/nuget/v/Sentry.AspNetCore.svg)](https://www.nuget.org/packages/Sentry.AspNetCore) | [![NuGet](https://img.shields.io/nuget/vpre/Sentry.AspNetCore.svg)](https://www.nuget.org/packages/Sentry.AspNetCore) | [![Documentation](https://img.shields.io/badge/documentation-sentry.io-green.svg)](https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/) |
| **Sentry.AspNetCore.Grpc** | [![Downloads](https://img.shields.io/nuget/dt/Sentry.AspNetCore.Grpc.svg)](https://www.nuget.org/packages/Sentry.AspNetCore.Grpc) | [![NuGet](https://img.shields.io/nuget/v/Sentry.AspNetCore.Grpc.svg)](https://www.nuget.org/packages/Sentry.AspNetCore.Grpc) | [![NuGet](https://img.shields.io/nuget/vpre/Sentry.AspNetCore.Grpc.svg)](https://www.nuget.org/packages/Sentry.AspNetCore.Grpc) | [![Documentation](https://img.shields.io/badge/documentation-sentry.io-green.svg)](https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/) |
| **Sentry.AspNet** | [![Downloads](https://img.shields.io/nuget/dt/Sentry.AspNet.svg)](https://www.nuget.org/packages/Sentry.AspNet) | [![NuGet](https://img.shields.io/nuget/v/Sentry.AspNet.svg)](https://www.nuget.org/packages/Sentry.AspNet) | [![NuGet](https://img.shields.io/nuget/vpre/Sentry.AspNet.svg)](https://www.nuget.org/packages/Sentry.AspNet) | [![Documentation](https://img.shields.io/badge/documentation-sentry.io-green.svg)](https://docs.sentry.io/platforms/dotnet/guides/aspnet) |
| **Sentry.EntityFramework** | [![Downloads](https://img.shields.io/nuget/dt/Sentry.EntityFramework.svg)](https://www.nuget.org/packages/Sentry.EntityFramework) | [![NuGet](https://img.shields.io/nuget/v/Sentry.EntityFramework.svg)](https://www.nuget.org/packages/Sentry.EntityFramework) | [![NuGet](https://img.shields.io/nuget/vpre/Sentry.EntityFramework.svg)](https://www.nuget.org/packages/Sentry.EntityFramework) | [![Documentation](https://img.shields.io/badge/documentation-sentry.io-green.svg)](https://docs.sentry.io/platforms/dotnet/guides/entityframework) |
| **Sentry.Serilog** | [![Downloads](https://img.shields.io/nuget/dt/Sentry.Serilog.svg)](https://www.nuget.org/packages/Serilog) | [![NuGet](https://img.shields.io/nuget/v/Sentry.Serilog.svg)](https://www.nuget.org/packages/Sentry.Serilog) | [![NuGet](https://img.shields.io/nuget/vpre/Sentry.Serilog.svg)](https://www.nuget.org/packages/Sentry.Serilog) | [![Documentation](https://img.shields.io/badge/documentation-sentry.io-green.svg)](https://docs.sentry.io/platforms/dotnet/guides/serilog) |
| **Sentry.Log4Net** | [![Downloads](https://img.shields.io/nuget/dt/Sentry.Log4Net.svg)](https://www.nuget.org/packages/Sentry.Log4Net) | [![NuGet](https://img.shields.io/nuget/v/Sentry.Log4Net.svg)](https://www.nuget.org/packages/Sentry.Log4Net) | [![NuGet](https://img.shields.io/nuget/vpre/Sentry.Log4Net.svg)](https://www.nuget.org/packages/Sentry.Log4Net) | [![Documentation](https://img.shields.io/badge/documentation-sentry.io-green.svg)](https://docs.sentry.io/platforms/dotnet/guides/log4net) |
| **Sentry.NLog** | [![Downloads](https://img.shields.io/nuget/dt/Sentry.NLog.svg)](https://www.nuget.org/packages/Sentry.NLog) | [![NuGet](https://img.shields.io/nuget/v/Sentry.NLog.svg)](https://www.nuget.org/packages/Sentry.NLog) | [![NuGet](https://img.shields.io/nuget/vpre/Sentry.NLog.svg)](https://www.nuget.org/packages/Sentry.NLog) | [![Documentation](https://img.shields.io/badge/documentation-sentry.io-green.svg)](https://docs.sentry.io/platforms/dotnet/guides/nlog) |
Expand All @@ -30,7 +31,6 @@ Sentry SDK for .NET

Sentry offers other integrations that are not part of this repository:

* [Sentry.EntityFramework](https://github.com/getsentry/sentry-dotnet-ef): EF 6 for validation errors and query as breadcrumbs.
* [Sentry.Minidump](https://github.com/getsentry/sentry-dotnet-minidump): Capture Minidumps on Windows, macOS and Linux.
* [Sentry.Xamarin](https://github.com/getsentry/sentry-xamarin): `Xamarin` native and `Xamarin.Forms` integration to get device data, breadcrumbs, and more.

Expand Down
21 changes: 21 additions & 0 deletions Sentry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Samples.AspNetCore.B
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.Samples.Aws.Lambda.AspNetCoreServer", "samples\Sentry.Samples.Aws.Lambda.AspNetCoreServer\Sentry.Samples.Aws.Lambda.AspNetCoreServer.csproj", "{274CEDC2-2129-469D-B269-649EFA2EF5E0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sentry.EntityFramework", "src\Sentry.EntityFramework\Sentry.EntityFramework.csproj", "{8B38F62E-0DD5-486F-96F5-2025AFB9B491}"
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 @@ -253,6 +259,18 @@ Global
{274CEDC2-2129-469D-B269-649EFA2EF5E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{274CEDC2-2129-469D-B269-649EFA2EF5E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{274CEDC2-2129-469D-B269-649EFA2EF5E0}.Release|Any CPU.Build.0 = Release|Any CPU
{8B38F62E-0DD5-486F-96F5-2025AFB9B491}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8B38F62E-0DD5-486F-96F5-2025AFB9B491}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8B38F62E-0DD5-486F-96F5-2025AFB9B491}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8B38F62E-0DD5-486F-96F5-2025AFB9B491}.Release|Any CPU.Build.0 = Release|Any CPU
{840B220E-68EC-4ECB-AEA1-67B0151F17FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{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 @@ -293,6 +311,9 @@ Global
{21599C29-C3D4-4DAC-A2D6-6C194600478F} = {77454495-55EE-4B40-A089-71B9E8F82E89}
{73FDCE53-75D1-4DCC-933A-8AB93A0E86EA} = {77454495-55EE-4B40-A089-71B9E8F82E89}
{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
40 changes: 40 additions & 0 deletions samples/Sentry.Samples.EntityFramework/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.Data.Common;
using System.Data.Entity;
using Sentry;
using Sentry.EntityFramework;

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>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/Sentry.Samples.EntityFramework/ef.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Data;
using Sentry.Extensibility;

namespace Sentry.EntityFramework.ErrorProcessors
{
/// <summary>
/// Exception processor for Entity Framework <see cref="DBConcurrencyException"/>.
/// </summary>
public class DbConcurrencyExceptionProcessor : SentryEventExceptionProcessor<DBConcurrencyException>
{
/// <summary>
/// Extracts RowCount and RowError from <see cref="DBConcurrencyException"/>.
/// </summary>
protected override void ProcessException(DBConcurrencyException exception, SentryEvent sentryEvent)
{
sentryEvent.SetExtra("Row Count", exception.RowCount);
sentryEvent.SetExtra("Row Error", exception.Row.RowError);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using Sentry.Extensibility;

namespace Sentry.EntityFramework.ErrorProcessors
{
/// <summary>
/// Exception processor for <see cref="DbEntityValidationException"/>.
/// </summary>
public class DbEntityValidationExceptionProcessor : SentryEventExceptionProcessor<DbEntityValidationException>
{
internal const string EntityValidationErrors = "EntityValidationErrors";

/// <summary>
/// Extracts details from <see cref="DbEntityValidationException"/> into the <see cref="SentryEvent"/>.
/// </summary>
protected override void ProcessException(DbEntityValidationException exception, SentryEvent sentryEvent)
{
var errorList = new Dictionary<string, List<string>>();
foreach (var error in exception.EntityValidationErrors.SelectMany(x=>x.ValidationErrors))
{
if (errorList.TryGetValue(error.PropertyName, out var list))
{
list.Add(error.ErrorMessage);
}
else
{
list = new List<string> { error.ErrorMessage };
errorList.Add(error.PropertyName, list);
}
}

sentryEvent.SetExtra(EntityValidationErrors, errorList);
}
}
}
15 changes: 15 additions & 0 deletions src/Sentry.EntityFramework/IQueryLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Sentry.EntityFramework
{
/// <summary>
/// A query logger interface.
/// </summary>
public interface IQueryLogger
{
/// <summary>
/// Logs a query with a related level.
/// </summary>
/// <param name="text">The query text.</param>
/// <param name="level">The level.</param>
void Log(string text, BreadcrumbLevel level = BreadcrumbLevel.Debug);
}
}
3 changes: 3 additions & 0 deletions src/Sentry.EntityFramework/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Sentry.EntityFramework.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010059964a931488bcdbd14657f1ee0df32df61b57b3d14d7290c262c2cc9ddaad6ec984044f761f778e1823049d2cb996a4f58c8ea5b46c37891414cb34b4036b1c178d7b582289d2eef3c0f1e9b692c229a306831ee3d371d9e883f0eb0f74aeac6c6ab8c85fd1ec04b267e15a31532c4b4e2191f5980459db4dce0081f1050fb8")]
24 changes: 24 additions & 0 deletions src/Sentry.EntityFramework/Sentry.EntityFramework.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net461;netstandard2.1</TargetFrameworks>
<PackageId>Sentry.EntityFramework</PackageId>
<AssemblyName>Sentry.EntityFramework</AssemblyName>
<RootNamespace>Sentry.EntityFramework</RootNamespace>
<Description>Official Entity Framework 6 integration for Sentry - Open-source error tracking that helps developers monitor and fix crashes in real time.</Description>
<PackageTags>$(PackageTags);EntityFramework;EF;EF6</PackageTags>
</PropertyGroup>

<ItemGroup>
<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>
67 changes: 67 additions & 0 deletions src/Sentry.EntityFramework/SentryCommandInterceptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Data.Common;
using System.Data.Entity.Infrastructure.Interception;

namespace Sentry.EntityFramework
{
/// <summary>
/// A command interceptor to augment data for Sentry.
/// </summary>
public class SentryCommandInterceptor : IDbCommandInterceptor
{
private readonly IQueryLogger _queryLogger;

/// <summary>
/// Creates a new instance of <see cref="SentryCommandInterceptor"/>.
/// </summary>
/// <param name="queryLogger"></param>
public SentryCommandInterceptor(IQueryLogger queryLogger) => _queryLogger = queryLogger;

/// <summary>
/// Logs a call to <see cref="NonQueryExecuting"/>.
/// </summary>
public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
=> Log(command, interceptionContext);

/// <summary>
/// No Op.
/// </summary>
public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext) { }

/// <summary>
/// Logs a call to <see cref="ReaderExecuting"/>.
/// </summary>
public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
=> Log(command, interceptionContext);

/// <summary>
/// No Op.
/// </summary>
public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext) { }

/// <summary>
/// Logs a call to <see cref="ScalarExecuting"/>.
/// </summary>
public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
=> Log(command, interceptionContext);

/// <summary>
/// Logs a call to <see cref="ScalarExecuted"/>.
/// </summary>
public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext) { }

/// <summary>
/// Logs the <see cref="DbCommand"/> with the specified context.
/// </summary>
public virtual void Log<T>(DbCommand command, DbCommandInterceptionContext<T> interceptionContext)
{
if (interceptionContext.Exception != null)
{
_queryLogger.Log(command.CommandText, BreadcrumbLevel.Error);
}
else
{
_queryLogger.Log(command.CommandText);
}
}
}
}
44 changes: 44 additions & 0 deletions src/Sentry.EntityFramework/SentryDatabaseLogging.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Data.Entity.Infrastructure.Interception;
using System.Threading;
using Sentry.Extensibility;

namespace Sentry.EntityFramework
{
/// <summary>
/// Sentry Database Logger
/// </summary>
public static class SentryDatabaseLogging
{
private static int Init = 0;

/// <summary>
/// Adds an instance of <see cref="SentryCommandInterceptor"/> to <see cref="DbInterception"/>
/// 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">Query Logger.</param>
[Obsolete("This method is called automatically by options.AddEntityFramework. This method will be removed in future versions.")]
public static SentryCommandInterceptor? UseBreadcrumbs(IQueryLogger? logger = null) => UseBreadcrumbs(logger);

internal static SentryCommandInterceptor? UseBreadcrumbs(
IQueryLogger? queryLogger = null,
bool initOnce = true,
IDiagnosticLogger? diagnosticLogger = null)
{
if (initOnce && Interlocked.Exchange(ref Init, 1) != 0)
{
diagnosticLogger?.LogWarning("{0}.{1} was already executed.",
nameof(SentryDatabaseLogging), nameof(UseBreadcrumbs));
return null;
}

diagnosticLogger?.LogInfo("{0}.{1} adding interceptor.",
nameof(SentryDatabaseLogging), nameof(UseBreadcrumbs));

queryLogger ??= new SentryQueryLogger();
var interceptor = new SentryCommandInterceptor(queryLogger);
DbInterception.Add(interceptor);
return interceptor;
}
}
}
Loading