Skip to content

feat: Provide the exception in the Hint passed to BeforeBreadcrumb - #5523

Open
jamescrosswell wants to merge 4 commits into
mainfrom
breadcrumb-exception-hint
Open

feat: Provide the exception in the Hint passed to BeforeBreadcrumb#5523
jamescrosswell wants to merge 4 commits into
mainfrom
breadcrumb-exception-hint

Conversation

@jamescrosswell

@jamescrosswell jamescrosswell commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator

Closes #5514

Summary

When a breadcrumb is created from an exception, the exception itself is now provided in the SentryHint passed to the SetBeforeBreadcrumb callback, under a new HintTypes.Exception key.

This mirrors the Java SDK, which puts the originating logging event into the Hint it passes to beforeBreadcrumb (see TypeCheckHint, e.g. logback:loggingEvent). Until now .NET only ever passed an empty hint for these, so a BeforeBreadcrumb callback could only filter exception breadcrumbs by string-matching the message.

options.SetBeforeBreadcrumb((breadcrumb, hint) =>
    hint.Items.TryGetValue(HintTypes.Exception, out var exception) && exception is MyException
        ? null       // drop it
        : breadcrumb);

Covered sites:

  • Hub.AddBreadcrumbForException — the automatic Exception breadcrumb left on the scope for every captured exception event.
  • Sentry.Extensions.Logging, Sentry.Serilog, Sentry.NLog and Sentry.Log4Net — breadcrumbs those integrations create for log entries that carry an exception (i.e. below MinimumEventLevel, where they don't defer to the Hub's breadcrumb).

Notes for review

  • HubExtensions gains a hint-carrying overload of the [EditorBrowsable(Never)] AddBreadcrumb(clock, …) method that the logging integrations use. It's a new overload rather than an added optional parameter, to avoid a binary-breaking change and overload ambiguity at existing call sites.
  • The Net4_8 API approval snapshot was hand-edited, since that TFM can't be built on macOS. The other three regenerated normally.

Relates to #5514. It does not close that issue: this gives users a way to filter exception breadcrumbs by type, but the SDK still leaves a breadcrumb for an exception that its own IExceptionFilter just dropped. That needs a separate change to AddBreadcrumbForException so it respects SentryOptions.ExceptionFilters.

Breadcrumbs created from an exception now carry that exception in the
SentryHint passed to the BeforeBreadcrumb callback, under the new
HintTypes.Exception key. This mirrors the Java SDK, which puts the
originating log event into the Hint it passes to beforeBreadcrumb.

Covers the automatic "Exception" breadcrumb the Hub leaves for captured
exception events, as well as the breadcrumbs created by the
Microsoft.Extensions.Logging, Serilog, NLog and log4net integrations.

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

codecov Bot commented Aug 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.72%. Comparing base (b96ff3b) to head (1b15609).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5523      +/-   ##
==========================================
+ Coverage   74.67%   74.72%   +0.04%     
==========================================
  Files         515      515              
  Lines       18909    18930      +21     
  Branches     3691     3692       +1     
==========================================
+ Hits        14120    14145      +25     
+ Misses       3904     3902       -2     
+ Partials      885      883       -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.

Comment thread src/Sentry.Extensions.Logging/SentryLogger.cs Outdated
Comment thread src/Sentry/Internal/Hub.cs Outdated
Addresses review feedback: the `exception is null ? null : new SentryHint(...)`
pattern repeated at each logging integration call site is now an internal
`ToHint()` extension on SentryExceptionExtensions.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@jamescrosswell
jamescrosswell marked this pull request as ready for review September 2, 2026 03:24
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Sep 2, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 1462662. Configure here.

Comment thread src/Sentry.Log4Net/SentryAppender.cs Outdated
Append already resolves the exception as `ExceptionObject ?? MessageObject
as Exception`, but the breadcrumb path only looked at ExceptionObject, so
`logger.Warn(ex)` produced a breadcrumb with no exception in its hint. Pass
the resolved exception through instead.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.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.

Breadcrumbs bypass IExceptionFilter, leaking filtered exception messages into later events

1 participant