Repository navigation
Description
Problem
An app can set Sentry up through a host integration (UseSentry in ASP.NET Core, the generic host, MAUI and others) and also send ILogger output through NLog, Serilog or log4net with that framework's Sentry integration. In that app, each ILogger entry reaches Sentry twice:
- through the
ILoggerproviders thatUseSentryregisters - through the framework's own
ILoggerprovider, then its pipeline, then the Sentry target, sink or appender
So one LogError("...") call creates two events and two breadcrumbs, and with EnableLogs on, two logs. Errors logged with an exception arrive once, because DuplicateEventDetectionEventProcessor drops the second capture of the same exception instance.
@ric-oliv tested this with Sentry.AspNetCore, Sentry.NLog and Sentry.Log4Net 7.0.0-rc.2, logging one LogError without an exception (comment):
| Setup | NLog events | NLog breadcrumbs | log4net events | log4net breadcrumbs |
|---|---|---|---|---|
UseSentry, then ClearProviders() and the framework's provider |
1 | 1 | 1 | 0 |
ClearProviders() and the framework's provider, then UseSentry |
2 | 2 | 2 | 1 |
Same, with Logging:Sentry:MinimumEventLevel and Logging:Sentry:MinimumBreadcrumbLevel set to None |
1 | 1 | 1 | 0 |
The same happens with NLog's AddNLog() and on the generic host with Sentry.Extensions.Hosting. It isn't new in v7, since 6.12.1 does the same. But more apps will hit it now, because the logging integrations can no longer initialize the SDK and the guides point users to a host integration. Whether an app gets duplicates depends on the order of its init calls, as the first two rows show.
Proposal
When a logging framework's Sentry integration already receives the app's ILogger entries, the host's Sentry ILogger providers stand down. They return a no-op logger and write one diagnostic warning. Each entry then reaches Sentry once, through the framework's integration. This is the pattern HostLoggerProvidersMarker already uses to make Logging.AddSentry() stand down for host integrations (#5694).
- The framework integrations register themselves. When
SentrySink,SentryTargetorSentryAppenderinitializes, it records itself in a small internal static registry inSentry. It can't be a DI marker, because the framework's own configuration creates these objects, not the container. These packages already useSentryinternals, such asUninitializedSdkWarning. - The host providers check lazily, on first log. They look at the registered
ILoggerProvidertypes for the framework bridges: Serilog'sSerilogLoggerProvider, NLog'sNLogLoggerProviderand the log4net provider (exact type names to confirm). If one is present and that framework's Sentry integration is in the registry, they stand down for events, breadcrumbs and logs.- The check can't run at registration time, because registration order is what makes today's outcome inconsistent.
- It can't run while our provider is being constructed either, because resolving
IEnumerable<ILoggerProvider>from inside it would recurse. - Once the check succeeds it's cached. Until then the providers keep checking the registry, which is cheap, so a target created after the app's first log is still picked up.
This covers every host integration that registers HostLoggerProvidersMarker: ASP.NET Core, the generic host, MAUI, Blazor WebAssembly and Google Cloud Functions. The result matches what apps get today from "UseSentry, then ClearProviders() and the framework's provider" (the first row above), whatever order the init calls are in.
The framework side has to be the one that wins. The ILogger entries already flow into the framework's pipeline, so nothing is lost. The reverse isn't possible, because the target, sink or appender can't tell which of its entries came from ILogger.
Why not deduplicate each entry
- No shared object: the two paths don't share any object per entry except the exception, which is already deduplicated.
SetFused/GetFusedneed an object to attach to. - Thread-static flags: these depend on which provider runs first, which is set by the app's registration order. They also break under NLog and Serilog async wrappers, which emit on another thread.
- Dropping identical items at send time: this would also drop genuine repeats, such as the same message logged in a loop.
Trade-offs
- The framework's routing and levels become the only gate for
ILoggerentries. For example, NLog rules that only sendMyApp.*to the Sentry target, or Serilog'sMinimumLevel.Override("Microsoft", LogEventLevel.Warning), would now dropMicrosoft.*entries that the host provider captured before. Unhandled exceptions still arrive through the host's middleware. - The host's settings stop applying to
ILoggerentries:MinimumEventLevel,MinimumBreadcrumbLevel,EnableLogsand log entry filters. The diagnostic warning should say so. - A Serilog sink on a different logger from the one the bridge writes to would lose entries silently. This is rare.
- The reverse direction isn't covered. Serilog's
UseSerilog(writeToProviders: true)sends Serilog's own events into Sentry'sILoggerprovider, which duplicates the other way. That needs a separate check on theILoggerFactorytype, or we accept it as an edge case.
The alternative is to detect the same situation but only warn, with the workaround of setting the Logging:Sentry levels to None, and change no behaviour. Standing down is a behaviour change, so it fits 7.0, where these integrations already changed how they start. If it lands, the docs note requested in getsentry/sentry-docs#19925 shrinks to one sentence explaining which side handles ILogger entries.
Related
- Generic host: ClearProviders() after UseSentry silently disables the ILogger integration, or the SDK entirely #5755:
ClearProviders()afterUseSentrysilently removes Sentry'sILoggerproviders. If that's fixed so Sentry's providers survive, the first row of the table would start duplicating too. - fix!: apps that call both UseSentry and Logging.AddSentry() no longer capture each error, breadcrumb and log twice #5694: the
HostLoggerProvidersMarkerprecedent.
Activity
linear-code commentedon Oct 22, 2026
NET-881
jamescrosswell commentedon Oct 27, 2026
Two things changed since this was filed:
ClearProviders()— it letsLogging.AddSentry()rebuild them on request, which these apps never call. Both rows of the table still behave as described.Minor: #5789's warning now fires on row 1, and its main advice — call
AddSentry()to add the providers back — would create the duplicates described here. Worth rewording if standing down lands.jamescrosswell commentedon Oct 27, 2026
For Serilog, the recommended setup doesn't duplicate.
builder.Host.UseSerilog(...)replaces theILoggerFactorywith Serilog's own, and that factory ignores everyILoggerProviderin the container, so the providersUseSentryregisters never run. Duplicates only appear when Serilog is added as a provider next to Sentry's, or when Serilog writes back into the providers.I tested the
Sentry.Samples.AspNetCore.Serilogsample onmain(Serilog.AspNetCore 6.1.0) against a mock Sentry endpoint. Each row is onelogger.LogError("...")with no exception:LogInformationbuilder.Host.UseSerilog(...)(recommended)builder.Host.UseSerilog(..., writeToProviders: true)builder.Logging.ClearProviders(); builder.Logging.AddSerilog();, thenUseSentrySo for Serilog this would only be a problem if someone initialised it using
Log.Logger.WriteTo.Sentryrather thanbuilder.Host.UseSentry... basically it doesn't work when people set it up wrong. I'll check NLog and log4net next.chore(samples): reproduce duplicate ILogger capture with NLog and log…
jamescrosswell commentedon Oct 27, 2026
After digging into this, I think it's working as designed and needs documenting, not an SDK change.
ILogger<T>writes throughILoggerFactory, and the default factory sends each entry to every registeredILoggerProvider.UseSentryregisters Sentry's providers. If the app registers another provider that also feeds Sentry, each entry reaches Sentry twice, as you'd expect from two providers. Users get caught becauseUseSentryadds its providers implicitly, so they may not realise they have two paths into Sentry.ClearProviders()only removes providers registered before it runs. WhenUseSentrycomes first it removes Sentry's providers too, which is why the order of the init calls decides the outcome in the table above.I reproduced every case below on
mainagainst a mock Sentry endpoint (repro: jamescrosswell/sentry-dotnet@repro/5763-ilogger-duplicates, seeSentry.Samples.AspNetCore.Basic).version7registers and filters these providers the same way.Footguns and correct setups
Serilog
ClearProviders()+AddSerilog(), thenUseSentryAddSerilog()adds Serilog as a provider next to Sentry's, so each entry reaches Sentry directly and again through the Serilog sink. WithUseSentryfirst it doesn't duplicate, but only becauseClearProviders()removed Sentry's providers.UseSerilog(..., writeToProviders: true)builder.Host.UseSerilog(...)ILoggerFactorywith Serilog's own, which ignores the registered providers. Sentry's providers never run, and everything reaches Sentry once, through the sink.NLog
builder.Host.UseNLog()/builder.Logging.AddNLog()(the default setup), thenUseSentryUseSentryfirst andClearProviders()between them it doesn't duplicate, but only because Sentry's providers were removed.builder.Host.UseNLog(new NLogAspNetCoreOptions { ReplaceLoggerFactory = true })UseSerilogdoes. One event in either order.log4net (
Microsoft.Extensions.Logging.Log4Net.AspNetCore, a third-party package)builder.Logging.AddLog4Net(), thenUseSentryILoggercapture with config:Logging:Sentry:MinimumEventLevelandLogging:Sentry:MinimumBreadcrumbLevelset toNone(Sentry:works too), plusLogging:Sentry:LogLevel:Defaultset toNone.LogLevelconfig (UseSentrygives it a delegate filter), so structured logs still duplicate until theLogLevelfilter turns off Sentry's log provider. Without theLogLevelsetting I got 1 event, 1 breadcrumb and 2 logs. With it, 1 of each. Levels set in code in theUseSentrycallback override the config.UseSentryfirst, thenClearProviders()+AddLog4Net(). Also 1 of each, but it depends on call order, and it triggers the fix(logging): warn when ClearProviders() removes the logger providers UseSentry added #5789 warning.Proposed changes
sentry-dotnet
Sentry.Samples.AspNetCore.Serilogalready usesUseSerilog(...), and there's no hosted NLog or log4net sample.version7warning text and XML docs:HostLoggerProvidersRemovedinSentryLoggingOptions.cs(fix(logging): warn when ClearProviders() removes the logger providers UseSentry added #5789) leads with "callAddSentry()… to add them back". For an app that sendsILoggerthrough NLog, Serilog or log4net, that advice creates these duplicates. Lead with that case instead: it's expected, and callingAddSentry()would send everything twice.AddSentry()remarks inLoggingBuilderExtensions.cs(both overloads) should say the same.sentry-docs (in getsentry/sentry-docs#19925 or a follow-up)
platform-includes/logs/setup/dotnet.mdx: both paragraphs say to leaveEnableLogsoff when another framework is anILoggerprovider. That stops duplicate logs, but events and breadcrumbs still duplicate, and the example given (builder.Logging.AddSerilog()) is itself a footgun. Point to the per-framework setup below instead.builder.Host.UseSerilog(...)for host apps, and warn againstAddSerilog()andwriteToProviders: truealongsideUseSentry.UseNLog(new NLogAspNetCoreOptions { ReplaceLoggerFactory = true }).docs: add an ASP.NET Core NLog sample and stop advising AddSentry() w…
ric-oliv commentedon Oct 29, 2026
@jamescrosswell Should we also close this issue now?