Skip to content

feat: Serilog sink no longer initializes the SDK - #5573

Open
jamescrosswell wants to merge 5 commits into
version7from
feat/no-init-from-logging-5245
Open

jamescrosswell wants to merge 5 commits into
version7from
feat/no-init-from-logging-5245

Conversation

@jamescrosswell

Copy link
Copy Markdown
Collaborator

The Serilog portion of #5245, following the design in this comment. The Sentry sink for Serilog now only configures the sink; Sentry has to be initialized separately via SentrySdk.Init, UseSentry, etc.

Part of #5245

Breaking changes

  • SentrySerilogOptions no longer derives from SentryOptions. It carries only sink settings: MinimumEventLevel, MinimumBreadcrumbLevel, FormatProvider, TextFormatter, RestrictedToMinimumLevel, LevelSwitch. Anything else (Dsn, Release, SampleRate, …) belongs on the options used to initialize Sentry.
  • SentrySerilogOptions.InitializeSdk is removed.
  • The WriteTo.Sentry(string dsn, …) overload is removed. Serilog.Settings.Configuration users passing dsn (or any other core SDK setting) in WriteTo args will no longer bind to a Sentry sink method.
  • ApplySerilogScopeToEvents() is renamed UseSerilog(), to match UseOpenTelemetry(). It returns void like its siblings and is now idempotent.

Before:

Log.Logger = new LoggerConfiguration()
    .WriteTo.Sentry(o =>
    {
        o.Dsn = "...";
        o.MinimumEventLevel = LogEventLevel.Error;
    })
    .CreateLogger();

After:

using var _ = SentrySdk.Init(o =>
{
    o.Dsn = "...";
    o.UseSerilog();
});

Log.Logger = new LoggerConfiguration()
    .WriteTo.Sentry(o => o.MinimumEventLevel = LogEventLevel.Error)
    .CreateLogger();

Notes for review

  • UseSerilog() registers the processor that copies Serilog LogContext properties onto events. It has to live on the SentryOptions used for init, because it enriches every event, not only ones the sink creates. To keep that discoverable, the sink logs a one-time diagnostic warning when it's missing (only visible with Debug = true).
  • The old WriteTo.Sentry(o => …) overload initialized the SDK but never registered that processor — only the dsn parameter overload did. That's why IntegrationTests.Simple snapshots gain inventory/MyTaskId tags: the test now calls UseSerilog(), and the processor is actually running.
  • The sink no longer implements IDisposable. It never owned the hub, so Log.CloseAndFlush() no longer disposes the SDK; disposing the handle from SentrySdk.Init does that.
  • SerilogAspNetSentrySdkTestFixture was initializing the SDK twice (once via WriteTo.Sentry(ValidDsn), then again via UseSentry); it now only initializes via UseSentry.
  • ApiApprovalTests.Run.Net4_8.verified.txt can't regenerate on macOS; it was byte-identical to the DotNet10_0 snapshot before this change, so it's a copy of the regenerated one.

NLog, log4net and Microsoft.Extensions.Logging follow separately. The generic host replacement for builder.Logging.AddSentry(dsn) is tracked in #5572.

🤖 Generated with Claude Code

The Sentry sink for Serilog now only configures the sink. Sentry must be
initialized separately (SentrySdk.Init, UseSentry, etc).

- SentrySerilogOptions no longer derives from SentryOptions and only
  carries sink settings; InitializeSdk is removed
- Remove the WriteTo.Sentry(string dsn, ...) overload
- Rename ApplySerilogScopeToEvents() to UseSerilog(), make it idempotent
- The sink logs a one-time diagnostic warning when UseSerilog() was not
  called on the options used to initialize Sentry

Part of #5245

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@jamescrosswell jamescrosswell added Breaking Change Binary/Source/Behavioral Breaking Changes. Serilog labels Sep 14, 2026
@github-actions github-actions Bot added the public API Additions/modifications to, or removals from, the public API surface area. label Sep 14, 2026
@codecov

codecov Bot commented Sep 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.44444% with 1 line in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (version7@21f45f0). Learn more about missing BASE report.

Files with missing lines Patch % Lines
src/Sentry.Serilog/SentrySink.cs 91.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             version7    #5573   +/-   ##
===========================================
  Coverage            ?   74.67%           
===========================================
  Files               ?      515           
  Lines               ?    18834           
  Branches            ?     3667           
===========================================
  Hits                ?    14064           
  Misses              ?     3893           
  Partials            ?      877           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jamescrosswell
jamescrosswell marked this pull request as ready for review September 21, 2026 05:06
@github-actions github-actions Bot added the risk: high PR risk score: high label Sep 21, 2026
@jamescrosswell
jamescrosswell added this pull request to stack #5603 September 21, 2026 05:12
@ric-oliv

Copy link
Copy Markdown
Member

Nice @jamescrosswell! Assuming we agree with this direction (Logging extension only configures the sink and does not inits the SDK), I think it's looking pretty good!

  • We should probably remove the Dsn (not used anymore) and the the EnableTracing (never used) properties from the appsettings.json example.
  • While testing the Serilog sample project (as a user upgrading and not initializing the Sentry SDK manually), I don't get any crash or warning... the SDK configures the sink, but no-ops on the execution. It might be worth checking if we should crash the SDK (or log via another channel) in this scenario.
  • Having the SDK initialized but not calling "UseSerilog()" is only going to show up in the logs as a warning, and only if we have debug = true. This feels a bit prone to issues with integrators forgetting to call "UseSerilog()" and not seeing any information at all about the issue. It would be great is we could somehow make the SDK itself register the SerilogScopeEventProcessor and avoid the call completely. Not sure if we can do this though.

Other than that, I believe we're good to merge this PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Breaking Change Binary/Source/Behavioral Breaking Changes. public API Additions/modifications to, or removals from, the public API surface area. risk: high PR risk score: high Serilog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants