Skip to content

Document capturing ASP.NET Core startup crashes with Serilog, NLog and log4net in v7.0.0 #5844

Closed

Description

@jamescrosswell

In 6.x, ASP.NET Core apps on Serilog's two-stage setup could report crashes during startup through the bootstrap logger: WriteTo.Sentry(dsn) initialized the SDK before the host existed, so the Log.Fatal(ex, "Host terminated unexpectedly") in Main's catch reached Sentry. #5573 removed that path in 7.0.0, and #5245 never named a replacement. On 7.0.0 a crash before UseSentry has initialized the SDK is dropped. Apps are already working around it by initializing Sentry inside the catch block, which loses the breadcrumbs logged before the crash.

The replacement is to call SentrySdk.Init at the top of Main and keep UseSentry. Neither the docs nor the samples show it.

Tested on version7

An ASP.NET Core app with Serilog, an early SentrySdk.Init (with UseSerilog()), then builder.UseSentry(...), sending to a local stand-in for Sentry:

Scenario Events Options used Breadcrumbs
Throws before Build() 1 early Init bootstrap logs
Throws after Build(), before Run() 1 early Init bootstrap logs
Run() fails (port already bound) 1 (the Log.Fatal duplicate is dropped) UseSentry none
Exception in a request, Serilog Log.Error in a request 1 each UseSentry request logs
Unhandled exception on a background thread 1 UseSentry —

UseSentry initializes when the hub is first resolved, during Run(), not Build(). It swaps in its hub and disposes the early one, which also unregisters the early hub's unhandled-exception handler (hence one event, not two, for the thread crash). Disposing the handle from the early Init at exit leaves the UseSentry hub alone.

What to document

  • The pattern itself, in the Serilog guide (docs/platforms/dotnet/guides/serilog/index.mdx) and the 7.x migration notes, as the replacement for WriteTo.Sentry(dsn) on the bootstrap logger.
  • appsettings.json hasn't been read when the early Init runs, so its DSN comes from SENTRY_DSN or code, as with the 6.x bootstrap logger. Crashes up to Run() carry the early Init's release and environment.
  • Options don't merge. UseSentry needs its own DSN (or the Sentry section) and its own UseSerilog().
  • Breadcrumbs from the bootstrap phase don't carry over to the UseSentry hub.
  • Document how to initialize Sentry in generic host apps for v7.0.0 #5707's migration note says to remove SentrySdk.Init when adding UseSentry. It should allow for this case.
  • Update samples/Sentry.Samples.AspNetCore.Serilog to show it.

Related: #5245, #5706, #5707

Activity

  1. added
    .NET
    on Nov 1, 2026
  2. added
    Next Major
    on Nov 1, 2026
  3. linear-code commented on Nov 1, 2026

    @linear-code
  4. jamescrosswell commented on Nov 2, 2026

    @jamescrosswell
    CollaboratorAuthor

    Changes made for this issue:

    The default setup in both guides is unchanged. The early Init is documented as an opt-in for apps that need to capture crashes before UseSentry starts Sentry.

  5. ric-oliv commented on Nov 3, 2026

    @ric-oliv
    Member

    Nice! Just a heads up, the same happens with NLog and log4net.
    Should we also open a PR to update Sentry.Samples.AspNetCore.NLog the same way? The console samples already call SentrySdk.Init first, and there's no ASP.NET Core sample for log4net.

    I've drafted sentry-docs#20408 with a migration guide item that covers all three. What do you think?

  6. jamescrosswell commented on Nov 3, 2026

    @jamescrosswell
    CollaboratorAuthor

    Should we also open a PR to update Sentry.Samples.AspNetCore.NLog the same way?

    I guess - let's expand the scope of this Issue to cover the NLog sample as well.

    there's no ASP.NET Core sample for log4net.

    I think log4net is pretty legacy - most folks who are using it will be using it with .NET framework so probably not work an ASP.NET Core sample. We can put a note in the docs about handling startup crashes with log4net though (e.g. in a background service app).

    I've drafted getsentry/sentry-docs#20408 with a migration guide item that covers all three. What do you think?

    Looks good - just one small change to the code sample so it will work with async as well. Left a review comment in the PR.

  7. changed the title [-]Document capturing ASP.NET Core startup crashes with Serilog in v7.0.0[/-] [+]Document capturing ASP.NET Core startup crashes with Serilog, NLog and log4net in v7.0.0[/+] on Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

.NETDocsFramework: ASP.NET CoreNext MajorSerilogState: Needs Discussion

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions