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

Exclude EF error messages from logging #1980

Merged
merged 12 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Split Android and Cocoa bindings into separate projects ([#1983](https://github.com/getsentry/sentry-dotnet/pull/1983))
- NuGet package `Sentry` now depends on `Sentry.Bindings.Android` for `net6.0-android` targets.
- NuGet package `Sentry` now depends on `Sentry.Bindings.Cocoa` for `net6.0-ios` and `net6.0-maccatalyst` targets.
- Exclude EF error message from logging ([#1980](https://github.com/getsentry/sentry-dotnet/pull/1980))

## 3.22.0

Expand Down Expand Up @@ -40,6 +41,7 @@
- Set default trace status to `ok` instead of `unknown_error` ([#1970](https://github.com/getsentry/sentry-dotnet/pull/1970))
- Fix reported error count on a crashed session update ([#1972](https://github.com/getsentry/sentry-dotnet/pull/1972))


## 3.21.0

_Includes Sentry.Maui Preview 3_
Expand Down
2 changes: 2 additions & 0 deletions src/Sentry.Extensions.Logging/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
[assembly: InternalsVisibleTo("Sentry.AspNetCore.Grpc, PublicKey=002400000480000094000000060200000024000052534131000400000100010059964a931488bcdbd14657f1ee0df32df61b57b3d14d7290c262c2cc9ddaad6ec984044f761f778e1823049d2cb996a4f58c8ea5b46c37891414cb34b4036b1c178d7b582289d2eef3c0f1e9b692c229a306831ee3d371d9e883f0eb0f74aeac6c6ab8c85fd1ec04b267e15a31532c4b4e2191f5980459db4dce0081f1050fb8")]
[assembly: InternalsVisibleTo("Sentry.AspNetCore.Grpc.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010059964a931488bcdbd14657f1ee0df32df61b57b3d14d7290c262c2cc9ddaad6ec984044f761f778e1823049d2cb996a4f58c8ea5b46c37891414cb34b4036b1c178d7b582289d2eef3c0f1e9b692c229a306831ee3d371d9e883f0eb0f74aeac6c6ab8c85fd1ec04b267e15a31532c4b4e2191f5980459db4dce0081f1050fb8")]
[assembly: InternalsVisibleTo("Sentry.Google.Cloud.Functions.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010059964a931488bcdbd14657f1ee0df32df61b57b3d14d7290c262c2cc9ddaad6ec984044f761f778e1823049d2cb996a4f58c8ea5b46c37891414cb34b4036b1c178d7b582289d2eef3c0f1e9b692c229a306831ee3d371d9e883f0eb0f74aeac6c6ab8c85fd1ec04b267e15a31532c4b4e2191f5980459db4dce0081f1050fb8")]
[assembly: InternalsVisibleTo("Sentry.DiagnosticSource.IntegrationTests, PublicKey=002400000480000094000000060200000024000052534131000400000100010059964a931488bcdbd14657f1ee0df32df61b57b3d14d7290c262c2cc9ddaad6ec984044f761f778e1823049d2cb996a4f58c8ea5b46c37891414cb34b4036b1c178d7b582289d2eef3c0f1e9b692c229a306831ee3d371d9e883f0eb0f74aeac6c6ab8c85fd1ec04b267e15a31532c4b4e2191f5980459db4dce0081f1050fb8")]

11 changes: 11 additions & 0 deletions src/Sentry.Extensions.Logging/SentryLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ internal static SentryEvent CreateEvent<TState>(LogLevel logLevel, EventId id, T
=> _options.MinimumEventLevel != LogLevel.None
&& logLevel >= _options.MinimumEventLevel
&& !IsFromSentry()
&& !IsEfExceptionMessage(eventId)
&& _options.Filters.All(
f => !f.Filter(
CategoryName,
Expand All @@ -156,6 +157,7 @@ internal static SentryEvent CreateEvent<TState>(LogLevel logLevel, EventId id, T
=> _options.MinimumBreadcrumbLevel != LogLevel.None
&& logLevel >= _options.MinimumBreadcrumbLevel
&& !IsFromSentry()
&& !IsEfExceptionMessage(eventId)
&& _options.Filters.All(
f => !f.Filter(
CategoryName,
Expand All @@ -180,4 +182,13 @@ private bool IsFromSentry()

return CategoryName.StartsWith("Sentry.", StringComparison.Ordinal);
}

internal static bool IsEfExceptionMessage(EventId eventId)
{
return eventId.Name is
"Microsoft.EntityFrameworkCore.Update.SaveChangesFailed" or
"Microsoft.EntityFrameworkCore.Query.QueryIterationFailed" or
"Microsoft.EntityFrameworkCore.Query.InvalidIncludePathError" or
"Microsoft.EntityFrameworkCore.Update.OptimisticConcurrencyException";
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Microsoft.EntityFrameworkCore;

namespace Sentry.DiagnosticSource.IntegrationTests.EF;

public class TestDbContext :
Expand All @@ -13,5 +11,7 @@ public class TestDbContext :
}

protected override void OnModelCreating(ModelBuilder model)
=> model.Entity<TestEntity>();
=> model.Entity<TestEntity>()
.HasIndex(u => u.Property)
.IsUnique();
}
14 changes: 14 additions & 0 deletions test/Sentry.DiagnosticSource.IntegrationTests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Global using directives

global using System.Reflection;
global using System.Runtime.CompilerServices;
global using System.Runtime.InteropServices;
global using DiffEngine;
global using Microsoft.Data.SqlClient;
global using Microsoft.EntityFrameworkCore;
global using Microsoft.EntityFrameworkCore.Diagnostics;
global using Microsoft.EntityFrameworkCore.Diagnostics.Internal;
global using Microsoft.Extensions.Logging;
global using Sentry.DiagnosticSource.IntegrationTests.EF;
global using Sentry.Extensions.Logging;
global using Sentry.Internal.DiagnosticSource;
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System.Runtime.InteropServices;
using DiffEngine;

namespace Sentry.DiagnosticSource.IntegrationTests;

public sealed class LocalDbFixture : IDisposable
Expand All @@ -14,7 +11,7 @@ public LocalDbFixture()
return;
}

SqlInstance = new SqlInstance(
SqlInstance = new(
name: "SqlListenerTests" + Namer.RuntimeAndVersion,
buildTemplate: TestDbBuilder.CreateTable);
}
Expand Down
7 changes: 7 additions & 0 deletions test/Sentry.DiagnosticSource.IntegrationTests/ModuleInit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#if NET6_0_OR_GREATER
public static class ModuleInit
{
[ModuleInitializer]
public static void Init() => VerifyEntityFramework.Enable();
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Verify.EntityFramework" Version="6.6.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.9" />
</ItemGroup>
Expand All @@ -22,6 +23,7 @@

<ItemGroup>
<PackageReference Include="LocalDb" Version="13.9.4" />
<ProjectReference Include="..\..\src\Sentry.Extensions.Logging\Sentry.Extensions.Logging.csproj" />
<ProjectReference Include="..\Sentry.Testing\Sentry.Testing.csproj" />
<ProjectReference Include="..\..\src\Sentry\Sentry.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
[
{
Source: {
Message: {
Message: Failed executing DbCommand,
Formatted:
Failed executing DbCommand
SET NOCOUNT ON;
INSERT INTO [TestEntities] ([Property])
VALUES (@p0);
SELECT [Id]
FROM [TestEntities]
WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();
},
Logger: Microsoft.EntityFrameworkCore.Database.Command,
Platform: csharp,
SentryExceptions: [],
Level: error,
Request: {},
Contexts: {},
User: {},
Environment: production,
Breadcrumbs: [
{
Message: Entity Framework Core initialized 'TestDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer:' with options: NoTracking ,
Data: {
eventId: Microsoft.EntityFrameworkCore.Infrastructure.ContextInitialized
},
Category: Microsoft.EntityFrameworkCore.Infrastructure
},
{
Message:
Executed DbCommand
SET NOCOUNT ON;
INSERT INTO [TestEntities] ([Property])
VALUES (@p0);
SELECT [Id]
FROM [TestEntities]
WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();,
Data: {
eventId: Microsoft.EntityFrameworkCore.Database.Command.CommandExecuted
},
Category: Microsoft.EntityFrameworkCore.Database.Command
}
],
Tags: {
commandText:
SET NOCOUNT ON;
INSERT INTO [TestEntities] ([Property])
VALUES (@p0);
SELECT [Id]
FROM [TestEntities]
WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();,
commandTimeout: 30,
commandType: Text,
eventId: Microsoft.EntityFrameworkCore.Database.Command.CommandError,
newLine:
,
parameters: @p0='?' (Size = 450)
}
}
},
{
Source: {
Name: my transaction,
Platform: csharp,
Operation: my operation,
Description: ,
Status: Ok,
IsSampled: true,
SampleRate: 1.0,
Request: {},
Contexts: {
trace: {
Operation: my operation,
Description: ,
Status: Ok,
IsSampled: true
}
},
User: {},
Environment: production,
Spans: [
{
IsFinished: true,
Operation: db.query,
Status: DeadlineExceeded,
IsSampled: true,
Extra: {
db.connection_id: Guid_1,
db.operation_id: Guid_2
}
},
{
IsFinished: true,
Operation: db.query,
Description:
SET NOCOUNT ON;
INSERT INTO [TestEntities] ([Property])
VALUES (@p0);
SELECT [Id]
FROM [TestEntities]
WHERE @@ROWCOUNT = 1 AND [Id] = scope_identity();

,
Status: InternalError,
IsSampled: true,
Extra: {
db.connection_id: Guid_1,
db.operation_id: Guid_2
}
}
],
IsFinished: true
}
}
]
Loading