Skip to content

fix(logging): warn when ClearProviders() removes the logger providers UseSentry added - #5789

Merged
jamescrosswell merged 4 commits into
version7from
fix/warn-when-logger-providers-removed
Oct 27, 2026
Merged

jamescrosswell merged 4 commits into
version7from
fix/warn-when-logger-providers-removed

Conversation

@ric-oliv

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

Copy link
Copy Markdown
Member

With Debug = true, the SDK now warns when the app removed the logger providers that UseSentry added, for example with ClearProviders(), and didn't add them back with builder.Logging.AddSentry(). Until now LogError calls silently stopped reaching Sentry.

The generic host checks during Build(), in the hook from #5772, so it also covers hosts that are never started. ASP.NET Core and Google Cloud Functions check in app.UseSentry() at startup. With both UseSentry calls, only ASP.NET Core checks.

The check reads the service collection first and stops if the host's providers are still there, so apps that replace the logger factory, as AddSerilog() does, get no providers created. Otherwise it looks for any Sentry logger provider, which covers Logging.AddSentry() after the clear.

Limitations:

  • The warning only shows with Debug = true, so NLog, Serilog and log4net apps that clear on purpose don't see it by default.
  • In ASP.NET Core it goes through the app's own ILogger, so an app with no providers left won't see it.
  • MAUI and Blazor WebAssembly have no check.
  • On the legacy WebHostBuilder, a clear inside Startup.ConfigureServices isn't detected, because that builder copies the service collection first.

Stacked on #5776.

Refs #5755

@codecov

codecov Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 76.21%. Comparing base (2194c92) to head (245cf66).
⚠️ Report is 3 commits behind head on version7.

Files with missing lines Patch % Lines
...c/Sentry.Extensions.Logging/HostLoggerProviders.cs 90.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           version7    #5789      +/-   ##
============================================
+ Coverage     76.17%   76.21%   +0.03%     
============================================
  Files           526      528       +2     
  Lines         19265    19318      +53     
  Branches       3729     3737       +8     
============================================
+ Hits          14676    14724      +48     
- Misses         3734     3735       +1     
- Partials        855      859       +4     

☔ 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 23, 2026 23:47
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Oct 23, 2026
Base automatically changed from fix/logging-addsentry-after-clearproviders to version7 October 26, 2026 23:12
ric-oliv and others added 4 commits October 27, 2026 12:24
After UseSentry, ClearProviders() removes Sentry's logger providers, and
without a later Logging.AddSentry() nothing says that ILogger entries no
longer reach Sentry.

The generic host now checks while the host is built, in the hook that
starts the SDK, and ASP.NET Core checks in app.UseSentry() when the host
starts. If the host's providers are gone and no Sentry logger provider
is registered, the SDK logs a warning through its diagnostic logger, so
it shows with Debug = true. The check reads the service collection first,
so apps that keep the registrations, such as those that replace the
logger factory with Serilog, never have providers created by it.

HostLoggerProviders now keeps the service collection it was added to,
which lets app.UseSentry() run the check and spares Logging.AddSentry()
capturing it.

Refs #5755

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

The legacy WebHostBuilder copies the service collection before
Startup.ConfigureServices, so checking the collection UseSentry used
brought back NullLoggerProviders there. Only the warning uses it now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The generic host now leaves the check to the other host integration,
as it already does for the logger providers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
IHostBuilder apps use ConfigureLogging(), so the warning now names the
logging builder and gives builder.Logging.AddSentry() as an example.

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 57933ef into version7 Oct 27, 2026
56 of 57 checks passed
@jamescrosswell
jamescrosswell deleted the fix/warn-when-logger-providers-removed branch October 27, 2026 01:40
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