Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Sentry.AspNetCore" Version="6.13.0" />
<PackageReference Include="Sentry.Serilog" Version="6.13.0" />
<PackageReference Include="Sentry.AspNetCore" Version="7.1.0" />
<PackageReference Include="Sentry.Serilog" Version="7.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
32 changes: 21 additions & 11 deletions src/Intility.Extensions.Logging.Sentry/LoggerBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.Extensions.Configuration;
using System;
using Microsoft.Extensions.Configuration;
using Sentry;
using Sentry.Serilog;
using Serilog;

Expand All @@ -14,25 +16,33 @@ public static class LoggerBuilderExtensions
/// <returns></returns>
public static ILoggerBuilder UseSentry(this ILoggerBuilder builder, string configSection = "Sentry")
{
// both SentryAspNetCoreOptions and SentrySerilogOptions can be bound to
// this section because they both inherit from Sentry.SentryOptions
var configuration = builder.Host.Configuration.GetSection(configSection);

if (string.IsNullOrWhiteSpace(configuration["Dsn"]))
{
return builder;
}

// configure serilog logger
builder.Configuration.WriteTo.Sentry((SentrySerilogOptions options) =>
// Since Sentry 7 the Serilog sink no longer initializes the SDK,
// so initialize it here, bound to the same configuration section.
var options = new SentryOptions();
configuration.Bind(options);

if (string.IsNullOrWhiteSpace(options.Environment))
{
configuration.Bind(options);
options.Environment = builder.Host.HostingEnvironment.EnvironmentName;
}

// Apply Serilog LogContext properties to Sentry events from the first event.
options.UseSerilog();

var sdk = SentrySdk.Init(options);

// Close the SDK when the host disposes the Serilog logger.
builder.Configuration.WriteTo.Sink(new SentryShutdownSink(sdk));

if (string.IsNullOrWhiteSpace(options.Environment))
{
options.Environment = builder.Host.HostingEnvironment.EnvironmentName;
}
});
// The sink captures log events and breadcrumbs.
builder.Configuration.WriteTo.Sentry(sinkOptions => configuration.Bind(sinkOptions));

return builder;
}
Expand Down
20 changes: 20 additions & 0 deletions src/Intility.Extensions.Logging.Sentry/SentryShutdownSink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using Serilog.Core;
using Serilog.Events;

namespace Intility.Extensions.Logging
{
/// <summary>
/// Keeps the Sentry SDK open for the lifetime of the Serilog logger.
/// Serilog disposes this sink, and with it the SDK, when the host
/// disposes the logger. The sink never receives log events.
/// </summary>
internal sealed class SentryShutdownSink(IDisposable sdk) : ILogEventSink, IDisposable
{
public void Emit(LogEvent logEvent)
{
}

public void Dispose() => sdk.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Intility.Extensions.Logging\Intility.Extensions.Logging.csproj" />
<ProjectReference Include="..\..\src\Intility.Extensions.Logging.Sentry\Intility.Extensions.Logging.Sentry.csproj" />
<ProjectReference Include="..\..\src\Intility.Logging.AspNetCore\Intility.Logging.AspNetCore.csproj" />
</ItemGroup>

Expand Down
52 changes: 52 additions & 0 deletions tests/Intility.Logging.Tests/SentryLoggingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Intility.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Sentry;

namespace Intility.Logging.Tests;

public class SentryLoggingTests
{
[Fact]
public void UseSentryInitializesAndClosesSdkWithHost()
{
var host = NewHost("https://ffffffffffffffffffffffffffffffff@localhost/1");

// UseIntilityLogging builds the Serilog pipeline lazily, when the logging
// factory is first resolved, as in a running application.
_ = host.Services.GetRequiredService<ILoggerFactory>();

Assert.True(SentrySdk.IsEnabled);

host.Dispose();

Assert.False(SentrySdk.IsEnabled);
}

[Fact]
public void UseSentryWithoutDsnDoesNotInitializeSdk()
{
using var host = NewHost(dsn: null);

_ = host.Services.GetRequiredService<ILoggerFactory>();

Assert.False(SentrySdk.IsEnabled);
}

private static IHost NewHost(string? dsn)
{
var settings = new Dictionary<string, string?>();

if (dsn is not null)
{
settings["Sentry:Dsn"] = dsn;
}

return Host.CreateDefaultBuilder()
.ConfigureAppConfiguration((_, config) => config.AddInMemoryCollection(settings))
.UseIntilityLogging((_, logging) => logging.UseSentry())
.Build();
}
}