Skip to content

fix(logging): don't send logs twice when Logging.AddSentry() is called more than once - #5786

Closed
ric-oliv wants to merge 1 commit into
fix/logging-addsentry-after-clearprovidersfrom
fix/logging-addsentry-called-twice
Closed

ric-oliv wants to merge 1 commit into
fix/logging-addsentry-after-clearprovidersfrom
fix/logging-addsentry-called-twice

Conversation

@ric-oliv

Copy link
Copy Markdown
Member

Calling builder.Logging.AddSentry() more than once now adds Sentry's logger providers once. Until now each call registered its own pair, so an app that calls it from Program.cs and from a shared setup helper sent every event, breadcrumb and structured log twice. That already happened without UseSentry, and #5776 opened a second way in: after UseSentry and ClearProviders(), each call rebuilds the host's providers.

AddSentry() now keeps the provider registrations it added, like HostLoggerProviders does, and skips them while they're still in the service collection. A ClearProviders() between two calls removes them, so the next call adds them again. Everything else is still registered on every call, so the options passed to each call still apply, in the same order as before.

Found while reviewing #5776, and stacked on it.

…s twice

Each Logging.AddSentry() call registered its own pair of providers, so an
app that calls it twice, for example from Program.cs and from a shared
setup helper, sent every event, breadcrumb and structured log twice.

AddSentry() now keeps the provider registrations it added and skips them
while they're still registered. A ClearProviders() between two calls
removes them, so the next call adds them again. Options, configuration
binding and filter rules are still registered on every call, so each
call's options still apply.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ric-oliv ric-oliv closed this Oct 23, 2026
@ric-oliv
ric-oliv deleted the fix/logging-addsentry-called-twice branch October 23, 2026 13:01
@codecov

codecov Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.87179% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.23%. Comparing base (f8fb00e) to head (e98831a).
⚠️ Report is 2 commits behind head on fix/logging-addsentry-after-clearproviders.

Files with missing lines Patch % Lines
...try.Extensions.Logging/LoggingBuilderExtensions.cs 90.90% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@                              Coverage Diff                               @@
##           fix/logging-addsentry-after-clearproviders    #5786      +/-   ##
==============================================================================
+ Coverage                                       76.22%   76.23%   +0.01%     
==============================================================================
  Files                                             528      529       +1     
  Lines                                           19307    19326      +19     
  Branches                                         3734     3736       +2     
==============================================================================
+ Hits                                            14716    14734      +18     
- Misses                                           3734     3735       +1     
  Partials                                          857      857              

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant