Skip to content

fix(logging): make Logging.AddSentry() work after UseSentry and ClearProviders() - #5776

Open
ric-oliv wants to merge 3 commits into
version7from
fix/logging-addsentry-after-clearproviders
Open

ric-oliv wants to merge 3 commits into
version7from
fix/logging-addsentry-after-clearproviders

Conversation

@ric-oliv

@ric-oliv ric-oliv commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

After UseSentry and then ClearProviders(), builder.Logging.AddSentry() now adds back the logger providers that UseSentry registered, so logger.LogError(...) reaches Sentry again. Until now, AddSentry() saw the marker that ClearProviders() left behind, installed two NullLoggerProviders and logged a warning telling people to remove the call. Reordering the calls was the only way out.

AddSentry() rebuilds the host's own providers rather than falling back to the plain SentryLoggerProvider pair. That way the host's options still apply, such as MinimumEventLevel set in UseSentry, and so do the filter rules that target the host's provider types, which ClearProviders() doesn't remove. In ASP.NET Core, one of those rules keeps the logger from capturing exceptions that UseExceptionHandler logs, so SentryMiddleware reports them with the right mechanism (#1284). Rebuilding the ASP.NET Core provider also starts the SDK during Build(), the same as without the clear.

To make that possible, each host integration now registers its providers through HostLoggerProviders, which replaces HostLoggerProvidersMarker. It keeps the provider factories and the exact descriptors it added. When AddSentry()'s providers are created, it checks the captured service collection for those descriptors, so calling AddSentry() before UseSentry still sends each error once. ASP.NET Core, Google Cloud Functions and MAUI now register their providers through factories instead of by type, and no extra provider joins the logging pipeline.

Two smaller changes, both logged through the SDK's diagnostic logger:

  • The warning for a redundant AddSentry() no longer tells people to remove the call. It says the call has no effect and its options are ignored.
  • A new warning says when the options passed to AddSentry() are ignored because it rebuilt the host's providers.

Apps that send logs through NLog or log4net and don't call Logging.AddSentry() are unaffected. UseSentry followed by ClearProviders() without AddSentry() still drops the ILogger integration without a word. A diagnostic warning for that comes next.

This is part 2 of the plan in #5755. It builds on #5772, which starts the SDK during Build().

Refs #5755

@codecov

codecov Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.15385% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.22%. Comparing base (2194c92) to head (49f5090).
⚠️ Report is 2 commits behind head on version7.

Files with missing lines Patch % Lines
...try.Extensions.Logging/LoggingBuilderExtensions.cs 88.88% 0 Missing and 2 partials ⚠️
...c/Sentry.Extensions.Logging/HostLoggerProviders.cs 94.73% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           version7    #5776      +/-   ##
============================================
+ Coverage     76.17%   76.22%   +0.04%     
============================================
  Files           526      528       +2     
  Lines         19265    19307      +42     
  Branches       3729     3734       +5     
============================================
+ Hits          14676    14716      +40     
  Misses         3734     3734              
- Partials        855      857       +2     

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

Base automatically changed from fix/generic-host-init-at-build to version7 October 23, 2026 00:35
@ric-oliv
ric-oliv marked this pull request as ready for review October 23, 2026 08:43
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Oct 23, 2026
After UseSentry and then ClearProviders(), Logging.AddSentry() found the
leftover marker, installed NullLoggerProviders and warned that the call was
redundant, so reordering the calls was the only fix.

Host integrations now register their providers through HostLoggerProviders,
which keeps the factories and the descriptors it added. AddSentry() checks the
captured service collection for those descriptors and, when the app removed
them, rebuilds the host's own providers. The host's options and the filters
that target its provider types keep applying, including the ASP.NET Core rule
for errors logged by UseExceptionHandler.

Refs #5755

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ric-oliv
ric-oliv force-pushed the fix/logging-addsentry-after-clearproviders branch from 479b8a6 to f8fb00e Compare October 23, 2026 08:54
@ric-oliv ric-oliv changed the title fix(logging): Logging.AddSentry() restores Sentry's logger providers after UseSentry and ClearProviders() fix(logging): make Logging.AddSentry() work after UseSentry and ClearProviders() Oct 23, 2026
…Logging.AddSentry()

Logging.AddSentry() rebuilds ASP.NET Core's providers because the generic
host's providers step aside. Only a unit test with substitute providers
covered that, so this runs it on a real WebApplication in both
registration orders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Four tests spelled out ConfigureServices(services => services.AddLogging(logging => logging.AddSentry())). LoggingAddSentry() reads like the ClearLoggerProviders() step next to it.

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

This branch has not been deployed

No deployments
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.

1 participant