Skip to content

fix(hosting): start the SDK during Build() even when the app clears or replaces logger providers - #5772

Merged
jamescrosswell merged 1 commit into
version7from
fix/generic-host-init-at-build
Oct 23, 2026
Merged

jamescrosswell merged 1 commit into
version7from
fix/generic-host-init-at-build

Conversation

@ric-oliv

Copy link
Copy Markdown
Member

The generic host's UseSentry now starts the SDK while the host is built, even when the app clears or replaces the logger providers. Until now, Sentry's logger provider was what started it during Build(). Calling ClearProviders() after UseSentry, or replacing the logger factory with Services.AddSerilog(), left nothing to start the SDK before the host started. A host that was built but never started captured nothing, and code in Program.cs between Build() and Run() ran without Sentry. One visible effect for those apps is that an invalid DSN now throws from Build() instead of StartAsync(), like it already did for apps with the default logging.

SentrySdkInitializer is an IConfigureOptions<HostOptions> that resolves Func<IHub>, which is what starts the SDK. The host's constructor reads IOptions<HostOptions> from Microsoft.Extensions.Hosting 3.1 onward, so it runs during Build() on both builders, whatever the app did to its logging. SentryHostedService still resolves Func<IHub> as well. If a host ever stopped reading HostOptions in its constructor, the SDK would still start before the other hosted services. The two start-order tests now remove the initializer to keep checking that.

HostOptions lives in Microsoft.Extensions.Hosting, not in the Abstractions package, so Sentry.Extensions.Hosting now depends on it: 3.1.0 for netstandard2.0 and 8.0.0 for .NET 8 and later. Generic host apps already reference it. I used the lowest version each target needs instead of one per runtime. Pinning 11.0.0 for net11.0 broke our GenericHost sample, which references 9.0.8, with NU1605, and apps that pin an older version would break the same way. In CI only the Windows net48 job runs the netstandard2.0 build, so I also checked it locally against Microsoft.Extensions.Hosting 3.1.0.

This is part 1 of the fix discussed in #5755. Logger providers removed by ClearProviders() still don't come back, and ASP.NET Core and Blazor WebAssembly aren't covered yet.

Refs #5755

… replaced

UseSentry relied on Sentry's logger provider to start the SDK while the host was built. When an
app called ClearProviders() after UseSentry, or replaced the logger factory with
Services.AddSerilog(), nothing started the SDK until the host started, so a host that was built
but never started captured nothing.

SentrySdkInitializer is an IConfigureOptions<HostOptions> that resolves Func<IHub>. The host's
constructor reads IOptions<HostOptions>, so it runs during Build() whatever the app did to its
logging. SentryHostedService keeps resolving Func<IHub> as a fallback.

HostOptions is in Microsoft.Extensions.Hosting, so the package now depends on it, at the lowest
version each target needs: one per runtime makes apps that reference an older version fail with
NU1605.

Refs #5755

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.18%. Comparing base (2194c92) to head (74f5618).
⚠️ Report is 1 commits behind head on version7.

Additional details and impacted files
@@            Coverage Diff            @@
##           version7    #5772   +/-   ##
=========================================
  Coverage     76.17%   76.18%           
=========================================
  Files           526      527    +1     
  Lines         19265    19268    +3     
  Branches       3729     3729           
=========================================
+ Hits          14676    14679    +3     
  Misses         3734     3734           
  Partials        855      855           

☔ 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.

@ric-oliv
ric-oliv marked this pull request as ready for review October 22, 2026 11:57
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Oct 22, 2026

@jamescrosswell jamescrosswell left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jamescrosswell
jamescrosswell merged commit 19bae26 into version7 Oct 23, 2026
33 of 34 checks passed
@jamescrosswell
jamescrosswell deleted the fix/generic-host-init-at-build branch October 23, 2026 00:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants