Skip to content

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

Merged
jamescrosswell merged 3 commits into
version7from
fix/logging-addsentry-after-clearproviders
Oct 26, 2026
Merged

jamescrosswell merged 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>

@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 172be49 into version7 Oct 26, 2026
38 checks passed
@jamescrosswell
jamescrosswell deleted the fix/logging-addsentry-after-clearproviders branch October 26, 2026 23:12
ric-oliv added a commit to getsentry/sentry-docs that referenced this pull request Oct 29, 2026
<!-- Keep the urgency section so automation can prioritize this PR. You
may delete other sections you don't need. -->

## DESCRIBE YOUR PR

This PR adds the migration guide for version 7 of the Sentry SDK for
.NET, ready for the public release candidate, `7.0.0-rc.2`. It follows
the pattern of the Flutter v10 guide from #19390:

- A full guide at `/platforms/dotnet/migration/v6-to-v7/`, built from
one include per change in `includes/migration/dotnet-v7/`.
- An interactive checklist at
`/platforms/dotnet/migration/v6-to-v7/interactive/` that renders the
same includes through `<MigrationGuide migration="dotnet-v7" />`.
- An agent prompt callout that points at the guide's `.md` URL.
- A "Migrating to 7.x" section at the top of the existing .NET migration
page, which links to both.

Both pages live under `common/`, so they render for every .NET guide,
such as ASP.NET Core, MAUI, WPF and Serilog. Xamarin uses a separate
SDK, so the new pages exclude it. I didn't move the 6.x, 4.x and 3.x
sections, so every existing URL and anchor keeps working and the PR
needs no redirects.

To register the collection, the PR adds `dotnet-v7` to the `MIGRATIONS`
map in the `MigrationGuide` component and to the validator's
`COLLECTIONS` map, with specs for both. These changes in
`src/components/migrationGuide/` and `scripts/` touch code that the
JavaScript and Flutter guides share, so they need an approval from the
docs team. For reference, msonnb approved the last change to the
component in #19729, and andreiborza approved the JavaScript interactive
guide that added it in #19076.

The checklist shows one step only on the MAUI guide. Every other step
applies to every .NET guide, because .NET apps combine integrations,
such as a Serilog sink in an ASP.NET Core app.

I checked the guide against `7.0.0-rc.2` from nuget.org and against the
`version7` branch at getsentry/sentry-dotnet@a56c63455. The guide
describes 7.0.0. Where rc.2 still behaves differently, the release
candidate note at the top says so.

### Why this goes live during the release candidate

I'd like to merge this as soon as it's approved, while 7.0 is still in
release candidate. 7.0.0 GA is planned for 2026-11-30. People who try
the release candidate need the guide now, and the agent prompt needs a
published `.md` URL to point at.

Two develop-docs rules pull in different directions here. [Managing
Linear
projects](https://develop.sentry.dev/sdk/getting-started/playbooks/coordination/managing-linear-projects/#visibility-for-major-releases)
says docs "MUST NOT go live before the SDK ships". The [breaking changes
playbook](https://develop.sentry.dev/sdk/getting-started/playbooks/sdk-lifecycle/breaking-changes/#6-update-docs-and-changelog)
says to publish the migration guide "before or alongside the major
release". I read the first rule as protecting users from docs for code
they can't get. Version `7.0.0-rc.2` is public on nuget.org, and the
guide's first block says it's a release candidate and names that
version. Other SDKs made the same call:

- The Python 2.0 guide merged on 2024-02-28, while only the `2.0.0a1`
alpha was out. Its note at the top said so.
- The JavaScript v11 guide merged on 2026-09-08 with `11.0.0-beta.1`
out, and its interactive version followed on 2026-09-11.
- The Flutter v10 guide merged on 2026-10-05 with `10.0.0-rc.2` out,
four days before 10.0.0.

### The release candidate note

The guide opens with one `<Alert>`. It says there is no stable 7.0 yet,
names `7.0.0-rc.2`, and says the rest of the .NET docs describe 6.x
until 7.0.0 ships. It also covers what only the release candidate has:

- `Logging.AddSentry()` after `ClearProviders()` doesn't add Sentry's
logger providers back yet. That came with getsentry/sentry-dotnet#5776,
after rc.2.
- `DecompressionMethods` still includes Brotli, which
getsentry/sentry-dotnet#5816 takes out of the default after rc.2. The
note says how to leave it out until then.
- An ASP.NET Core app that sends `ILogger` entries through NLog crashes
with `Debug` on, tracked in getsentry/sentry-dotnet#5822. The guide's
verify step turns `Debug` on, so the note says to leave it off in those
apps.
- Its `net11.0` builds depend on the .NET 11 release candidate versions
of three `Microsoft.Extensions` packages.

The rest of the guide describes 7.0.0 as released. At GA, I'll delete
that one block from `includes/dotnet-v7-migration-overview.mdx`, and
nothing else needs to change.

### What this PR leaves to other PRs

This PR only adds the guide and what it needs. The reference pages are
in other PRs, and this one doesn't need any of them to merge first:

- Logs and the logging integrations: #19925
- `Microsoft.Extensions.Logging` and ASP.NET Core: #19961
- OpenTelemetry propagation: #19924
- Release and environment: #19960
- Hangfire monitor schedules: #19781

Three links in the guide point to the "Apps That Call UseSentry"
sections that #19925 adds to the Serilog, NLog and log4net guides. Until
#19925 merges, they open those guides at the top, and the migration
guide sums up the same setups itself.

There is no page for the new `Sentry.Extensions.Hosting` package yet.
I'll link it from the guide once that page exists.

Refs https://linear.app/getsentry/issue/NET-860
Refs getsentry/sentry-dotnet#5710

## IS YOUR CHANGE URGENT?

Help us prioritize incoming PRs by letting us know when the change needs
to go live.
Select exactly one option. For deadlines, replace `YYYY-MM-DD` with the
due date. You can update this information later by editing the PR
description.

- [x] Urgent deadline (GA date, etc.): 2026-11-30
- [ ] Other deadline: YYYY-MM-DD
- [ ] No deadline: Not urgent, can wait up to 1 week+

## PRE-MERGE CHECKLIST

_Make sure you've checked the following before merging your changes:_

- [ ] Checked Vercel preview for correctness, including links
- [ ] PR was reviewed and approved by any necessary SMEs (subject matter
experts)
- [ ] PR was reviewed and approved by a member of the [Sentry docs
team](https://github.com/orgs/getsentry/teams/docs)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: James Crosswell <jamescrosswell@users.noreply.github.com>
Co-authored-by: Alex Krawiec <alex.krawiec@sentry.io>
ric-oliv added a commit to getsentry/sentry-docs that referenced this pull request Nov 3, 2026
<!-- Keep the urgency section so automation can prioritize this PR. You
may delete other sections you don't need. -->

## DESCRIBE YOUR PR

Version `7.0.0-rc.3` of the Sentry SDK for .NET is out, so this PR
updates the release candidate note at the top of the 7.x migration guide
from #20143.

The note listed three things that work differently on `7.0.0-rc.2`. All
three fixes are in rc.3, so I removed the list:

- `Logging.AddSentry()` after `ClearProviders()` adds Sentry's logger
providers back, from getsentry/sentry-dotnet#5776.
- `DecompressionMethods` leaves out Brotli by default, from
getsentry/sentry-dotnet#5816.
- An ASP.NET Core app that sends `ILogger` entries through NLog no
longer crashes with `Debug` on, from getsentry/sentry-dotnet#5823.

The note no longer names a release candidate. It points at the latest
one on NuGet instead, so another release candidate before GA won't need
a docs PR.

I also fixed two gaps in the NU1605 paragraph:

- `Sentry.AspNetCore.Blazor.WebAssembly` depends on the .NET 11 version
of `Microsoft.AspNetCore.Components.WebAssembly`, so a Blazor
WebAssembly app that pins 10.0.x fails to restore too. The paragraph now
names that package.
- An app that pins an older .NET 11 release candidate, such as
`11.0.0-rc.2`, fails the same way as one that pins 10.0.x. The paragraph
now says "an older version" and that the error message shows the version
to use.

I checked this against `7.0.0-rc.3` from nuget.org:

- The default `DecompressionMethods` is `GZip, Deflate`.
- A .NET 11 app fails to restore with NU1605 when it pins
`Microsoft.Extensions.Http` 10.0.0 or 11.0.0-rc.2.
- A Blazor WebAssembly app fails the same way when it pins
`Microsoft.AspNetCore.Components.WebAssembly` 10.0.0.
- All three fix PRs are in the `7.0.0-rc.3` tag.

No other page in the guide names a release candidate version. The whole
alert still comes out at 7.0.0 GA.

Refs https://linear.app/getsentry/issue/NET-860
Refs getsentry/sentry-dotnet#5710

## IS YOUR CHANGE URGENT?

Help us prioritize incoming PRs by letting us know when the change needs
to go live.
Select exactly one option. For deadlines, replace `YYYY-MM-DD` with the
due date. You can update this information later by editing the PR
description.

- [ ] Urgent deadline (GA date, etc.): YYYY-MM-DD
- [ ] Other deadline: YYYY-MM-DD
- [x] No deadline: Not urgent, can wait up to 1 week+

## PRE-MERGE CHECKLIST

_Make sure you've checked the following before merging your changes:_

- [ ] Checked Vercel preview for correctness, including links
- [ ] PR was reviewed and approved by any necessary SMEs (subject matter
experts)
- [ ] PR was reviewed and approved by a member of the [Sentry docs
team](https://github.com/orgs/getsentry/teams/docs)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
jamescrosswell added a commit to getsentry/sentry-docs that referenced this pull request Nov 10, 2026
> [!NOTE]
> This targets `dotnet-v7`, which collects the .NET docs for
sentry-dotnet 7.0.0. #20641 merges that branch into
`master` when 7.0.0 ships, so this PR can merge into `dotnet-v7` as soon
as it's approved.

## DESCRIBE YOUR PR

Adds a Generic Host guide for `Sentry.Extensions.Hosting`, which
sentry-dotnet 7.0.0 adds (getsentry/sentry-dotnet#5739). In 7.0.0,
`Logging.AddSentry()` no longer initializes the SDK. Worker services,
console apps on the generic host, Windows services and Azure Functions
(isolated) apps call `UseSentry` from this package instead. The page is
written for 7.x. The docs aren't versioned, so this goes live with the
rest of the 7.0.0 docs.

The guide follows the quick start template from
#19990 and covers:

- `UseSentry` on `IHostApplicationBuilder` and `IHostBuilder`, the
`Sentry` and `Logging:Sentry` sections, and why not to call
`Logging.AddSentry()` as well
- the host environment, `"Dsn": ""`, and when the SDK starts (`Build()`)
and closes (`ApplicationStopped`)
- `ClearProviders()` ordering (getsentry/sentry-dotnet#5772,
getsentry/sentry-dotnet#5776, getsentry/sentry-dotnet#5789,
getsentry/sentry-dotnet#5821)
- ASP.NET Core and MAUI apps, and the static `UseSentry` call that
Aspire's `AddServiceDefaults` needs
- the .NET Framework gap, with the `SentrySdk.Init` and
`Logging.AddSentry()` fallback and its `HubAdapter.Instance`
registrations (getsentry/sentry-dotnet#5740)
- replacing `SentrySdk.Init`, briefly. The migration guide
(#20143) covers upgrades.

It also adds
`platform-includes/configuration/config-intro/dotnet.extensions-hosting.mdx`,
so the guide's Options page starts with `UseSentry` instead of
`SentrySdk.Init`.

Closes getsentry/sentry-dotnet#5707

### Notes for review

- **Registration.** A new .NET guide only needs its `index.mdx`. The
platform key `dotnet.extensions-hosting` comes from the folder name,
guides without `sidebar_order`, like the other .NET guides, are listed
by title, and the icon falls back to the .NET one, as for the
Microsoft.Extensions.Logging, Serilog and NLog guides. The page is new,
so it needs no redirects.
- **Package version.** The install snippets use
`packages.version('sentry.dotnet')`, because the release registry has no
`sentry.dotnet.extensions.hosting` entry yet. Until 7.0.0 is out, the
preview shows 6.12.1, which doesn't exist for this package. Once the
registry has the package, switch to its own key. The `sdk:` frontmatter
already uses `sentry.dotnet.extensions.hosting` (from `Constants.cs`).
Without a registry entry, the page just has no package details box.
- **Shared includes that render wrongly for
`dotnet.extensions-hosting`.** I left them alone, since other PRs own
them:
- `platform-includes/logs/setup/dotnet.mdx` falls into the
`SentrySdk.Init` section instead of `UseSentry` with `EnableLogs`.
#19925 is adding the key to the host sections.
- `platform-includes/logs/options/dotnet.mdx` shows the generic
`EnableLogs` text and leaves out "Configure Microsoft.Extensions.Logging
Filters". Same PR.
- `platform-includes/logs/usage/dotnet.mdx` shows only the
`SentrySdk.Logger` APIs. The key belongs in the `ILogger` section too.
- `platform-includes/logs/requirements/dotnet.mdx` says logs need SDK
version 5.14.0. This package starts at 7.0.0.
- **Additions to the brief from the `version7` source:**
- `"Dsn": ""` turns the SDK off, but `SENTRY_DSN` still wins over an
empty `Dsn` (`SettingLocator.GetDsn`). The page says so.
- `Logging:Sentry` only accepts `LogLevel`, `MinimumBreadcrumbLevel` and
`MinimumEventLevel`. Any other key makes `Build()` throw
`NotSupportedException`. With no DSN anywhere, `Build()` throws
`ArgumentNullException`.
- Calling `Sentry.AspNetCore`'s `UseSentry` twice still sends each error
twice (a follow-up in getsentry/sentry-dotnet#5739). The Aspire section
says not to call it again in `Program.cs`.
- **Azure Functions** gets one line in the intro. The Azure Functions
guide still shows `logging.AddSentry(...)`. #19925
owns it.
- **Links to add once the other PRs merge:**
- Microsoft.Extensions.Logging guide: send generic host apps here, from
the "If you use a host integration such as ASP.NET Core or MAUI"
paragraph. Its `Sentry.Samples.GenericHost` sample link belongs here
too, since the sample now calls `UseSentry`.
  - Azure Functions guide: link here from the `UseSentry` setup.
- `platform-includes/logs/integrations/dotnet.mdx`: add Generic Host to
the list.
- `docs/product/logs/getting-started/index.mdx` and
`docs/product/metrics/getting-started/index.mdx`: add Generic Host to
the .NET lists.
- Migration guide, "Generic Host Apps Call UseSentry": link to this
page.
- The sample link points at `main`, where `Sentry.Samples.GenericHost`
keeps the 6.x setup until `version7` is merged.
- The C# snippets compile against `version7`, on net10.0, and on net48
for the `IHostBuilder` example and the .NET Framework fallback. On
net48, `UseSentry` on `HostApplicationBuilder` fails with CS1929, as the
page says.

## IS YOUR CHANGE URGENT?

Help us prioritize incoming PRs by letting us know when the change needs
to go live.
Select exactly one option. For deadlines, replace `YYYY-MM-DD` with the
due date. You can update this information later by editing the PR
description.

- [x] Urgent deadline (GA date, etc.): 2026-11-30
- [ ] Other deadline: YYYY-MM-DD
- [ ] No deadline: Not urgent, can wait up to 1 week+

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Ricardo Colombo Oliveira <github@ricoliv.com>
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.

Generic host: ClearProviders() after UseSentry silently disables the ILogger integration, or the SDK entirely

2 participants