Skip to content

Start the SDK during Build() in ASP.NET Core and Blazor WebAssembly, as the generic host does #5815

Open

Description

@jamescrosswell

Follow-up to #5772, which made the generic host's UseSentry start the SDK while the host is built, so it no longer depends on Sentry's logger providers surviving. ASP.NET Core and Blazor WebAssembly still do.

SentryAspNetCoreLoggerProvider's constructor takes IHub, so constructing the provider is what starts the SDK during Build(). After ClearProviders() the remaining triggers — SentryStartupFilter and ApplicationBuilderExtensions' GetService<Func<IHub>>() — both run when the app starts, which leaves the window between Build() and Run() unprotected:

var app = builder.Build();
app.Services.GetRequiredService<Migrator>().Migrate();   // no Sentry here
app.Run();

Running migrations or seeding there is common, and it's the code most likely to fail on a new deployment.

Unlike the generic host case in #5755, this isn't about a host that is built and never started — a web app is effectively always started, so the SDK does come up eventually. The gap is specifically the code that runs before it does.

#5776 narrows this: rebuilding the ASP.NET Core provider also starts the SDK during Build(), but only for apps that call Logging.AddSentry() after ClearProviders(). An app that clears the providers and doesn't call it still has the gap.

Suggested approach

The mechanism from #5772 should transfer directly to ASP.NET Core — register SentrySdkInitializer as an IConfigureOptions<HostOptions> from the ASP.NET Core UseSentry paths, since Host's constructor reads IOptions<HostOptions> there too.

Blazor WebAssembly needs its own answer: WebAssemblyHostBuilder isn't the generic host and doesn't construct a Host, so there is no HostOptions resolution to hook. Worth checking what, if anything, it guarantees to resolve during Build().

Ideally this lands before 7.0 GA. The inconsistency matters most while the major is being cut — shipping it with the generic host fixed and the web host not is harder to explain than fixing both at once.

Refs #5755, #5721, #5772, #5776

Also in scope: the missing warning checks

#5789 added a warning (with Debug = true) when the app removed Sentry's logger providers and didn't add them back with builder.Logging.AddSentry(). It covers the generic host, ASP.NET Core and Google Cloud Functions. Two gaps were left, and they belong with the work above rather than in a separate issue:

  • Start the SDK during Build() in ASP.NET Core
  • Start the SDK during Build() in Blazor WebAssembly, or work out what the equivalent hook is
  • Add the removed-providers warning to MAUI
  • Add the removed-providers warning to Blazor WebAssembly

The last two need the same answer as the first two for Blazor WebAssembly, since neither the initialisation hook nor the warning has anywhere obvious to live there.

Refs #5755 (closed — see the summary comment there for what shipped and what was decided against)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions