Repository navigation
Description
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 forWriteTo.Sentry(dsn)on the bootstrap logger. appsettings.jsonhasn't been read when the earlyInitruns, so its DSN comes fromSENTRY_DSNor code, as with the 6.x bootstrap logger. Crashes up toRun()carry the earlyInit's release and environment.- Options don't merge.
UseSentryneeds its own DSN (or theSentrysection) and its ownUseSerilog(). - Breadcrumbs from the bootstrap phase don't carry over to the
UseSentryhub. - Document how to initialize Sentry in generic host apps for v7.0.0 #5707's migration note says to remove
SentrySdk.Initwhen addingUseSentry. It should allow for this case. - Update
samples/Sentry.Samples.AspNetCore.Serilogto show it.
Activity
linear-code commentedon Nov 1, 2026
NET-897
docs(dotnet): Document capturing startup crashes on the Generic Host
docs(dotnet): Document capturing startup crashes with Serilog
jamescrosswell commentedon Nov 2, 2026
Changes made for this issue:
samples/Sentry.Samples.AspNetCore.Serilogto initialize Sentry early, with a comment explaining why it's initialized twice.SentrySdk.Init" note.The default setup in both guides is unchanged. The early
Initis documented as an opt-in for apps that need to capture crashes beforeUseSentrystarts Sentry.ric-oliv commentedon Nov 3, 2026
Nice! Just a heads up, the same happens with NLog and log4net.
Should we also open a PR to update
Sentry.Samples.AspNetCore.NLogthe same way? The console samples already callSentrySdk.Initfirst, 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?
jamescrosswell commentedon Nov 3, 2026
I guess - let's expand the scope of this Issue to cover the NLog sample as well.
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).
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.
[-]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[/+]docs(dotnet): Document capturing startup crashes with NLog and log4net
docs(dotnet): Capture startup crashes in a catch for async Main
docs(dotnet): Document startup crashes in the 7.x migration guide (#2…
docs(samples): capture startup crashes in the ASP.NET Core NLog sample (
docs(samples): capture startup crashes in the ASP.NET Core Serilog sa…
chore(samples): clarify the Serilog sink comment in the ASP.NET Core …
docs(dotnet): Update logging docs for SDK 7.0.0 (#19925)