Skip to content

feat(serilog): support restrictedToMinimumLevel when configuring Serilog in code#5181

Merged
jamescrosswell merged 5 commits into
mainfrom
feat/serilog-restricted-to-minimum-level
Jul 16, 2026
Merged

feat(serilog): support restrictedToMinimumLevel when configuring Serilog in code#5181
jamescrosswell merged 5 commits into
mainfrom
feat/serilog-restricted-to-minimum-level

Conversation

@jamescrosswell

@jamescrosswell jamescrosswell commented May 4, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes #4957

  • Added RestrictedToMinimumLevel and LevelSwitch options to SentrySerilogOptions, plus matching restrictedToMinimumLevel and levelSwitch parameters on the two parameterized Sentry() extension method overloads on LoggerSinkConfiguration
  • These are passed through to Serilog's underlying Sink() call, enabling per-sink minimum level filtering to be set in code
  • Previously, per-sink minimum levels could only be configured via appsettings.json; this change unblocks MAUI users who configure Serilog in code

Usage

.WriteTo.Sentry(o =>
{
    o.MinimumBreadcrumbLevel = LogEventLevel.Debug;
    o.MinimumEventLevel = LogEventLevel.Error;
    // Only Warning and higher reach the Sentry sink
    o.RestrictedToMinimumLevel = LogEventLevel.Warning;
})

Docs

🤖 Generated with Claude Code

…log in code

Add `restrictedToMinimumLevel` and `levelSwitch` parameters to all three
`Sentry()` extension method overloads on `LoggerSinkConfiguration`, passing
them through to Serilog's underlying `Sink()` call. This allows per-sink
minimum level filtering to be configured in code (e.g. for MAUI users) rather
than only via appsettings.json.

Fixes #4957

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@jamescrosswell
jamescrosswell requested a review from Flash0ver as a code owner May 4, 2026 04:11
Comment thread test/Sentry.Serilog.Tests/SentrySerilogSinkExtensionsTests.cs Outdated
@codecov

codecov Bot commented May 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.13%. Comparing base (32a55e3) to head (3516b83).
⚠️ Report is 15 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5181      +/-   ##
==========================================
+ Coverage   74.06%   74.13%   +0.07%     
==========================================
  Files         501      506       +5     
  Lines       18113    18255     +142     
  Branches     3521     3564      +43     
==========================================
+ Hits        13415    13533     +118     
- Misses       3838     3852      +14     
- Partials      860      870      +10     

☔ View full report in Codecov by Sentry.
📢 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.

@jamescrosswell
jamescrosswell marked this pull request as draft May 4, 2026 05:25
jamescrosswell and others added 4 commits May 5, 2026 13:50
…rySerilogOptions

Move restrictedToMinimumLevel and levelSwitch out of separate overload
parameters and into SentrySerilogOptions as first-class properties, consistent
with how all other Serilog options are handled. ConfigureSentrySerilogOptions
now sets them alongside the rest of the configuration, and the
Action<SentrySerilogOptions> overload reads them from options when calling
loggerConfiguration.Sink().

This is the idiomatic pattern for the codebase and means MAUI users can
configure everything uniformly via the options callback.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ilter test

Use a mock hub with an internal SentrySink and wire it to Serilog via
loggerConfiguration.Sink(sink, options.RestrictedToMinimumLevel, ...) — the
same call the extension method makes — so we can assert that Warning events
are filtered by Serilog before reaching the hub, while Error events get through.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@@ -21,7 +23,7 @@ namespace Serilog
public static class SentrySinkExtensions
{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Adding optional parameters to public Sentry() overloads is a binary-breaking change

Adding new optional parameters (restrictedToMinimumLevel, levelSwitch) to the existing public Sentry() extension methods changes their metadata signatures. While source-compatible, this is binary-breaking in .NET: consumers compiled against a previous version of Sentry.Serilog will get a MissingMethodException at runtime until they recompile. For a public library this is an API contract change that warrants senior engineer review and, ideally, preservation of the prior signatures via additional overloads instead of in-place parameter additions.

Verification

Read src/Sentry.Serilog/SentrySinkExtensions.cs and confirmed the two affected overloads (lines 86-113 and 180-187) had new optional parameters appended rather than being introduced as new overloads. Verified that the API approval snapshot reflects the same signature change at lines 24 and 49-53 of the .verified.txt hunk. .NET's binding to optional parameters is resolved at the call site at compile time, so adding optional parameters changes the method's metadata token and breaks binary compatibility, even though source compiles unchanged.

Identified by Warden code-review · PB4-XBA

@jamescrosswell jamescrosswell May 7, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@Flash0ver I'd rather go with a binary breaking change here (it's source compatible). The signatures get really messy if we try to add overloads (there are domino effects).

I don't think there are many people swapping out the Sentry dll without recompiling their apps when they upgrade (I'd venture to say probably none)... and accessing functionality like this via reflection also seems highly improbable.

How do you feel about that?

@jamescrosswell
jamescrosswell marked this pull request as ready for review May 7, 2026 03:26
@dingsdax
dingsdax requested review from dingsdax and removed request for Flash0ver July 15, 2026 22:48
@jamescrosswell
jamescrosswell merged commit 49fd45e into main Jul 16, 2026
54 of 56 checks passed
@jamescrosswell
jamescrosswell deleted the feat/serilog-restricted-to-minimum-level branch July 16, 2026 03:36
dingsdax added a commit to getsentry/sentry-docs that referenced this pull request Jul 20, 2026
## DESCRIBE YOUR PR

Documents the new Serilog per-sink minimum level configuration added in
getsentry/sentry-dotnet#5181 (fixes getsentry/sentry-dotnet#4957),
shipping in Sentry.Serilog 6.8.0.

- Adds a "Filtering Which Log Events Reach Sentry" section to the
Serilog guide explaining how the per-sink minimum level differs from
`MinimumBreadcrumbLevel`/`MinimumEventLevel`, with code and
`appsettings.json` examples
- Documents the new `RestrictedToMinimumLevel` and `LevelSwitch` options
- Notes that configuring this in code requires Sentry.Serilog 6.8.0+
(previously only possible via `appsettings.json`)

Note: the C# snippets were compiled against `sentry-dotnet@main` to
confirm they build. The version number in the alert assumes the next
release is 6.8.0 — please double-check once it's tagged.

## IS YOUR CHANGE URGENT?

Help us prioritize incoming PRs by letting us know when the change needs
to go live.
- [ ] Urgent deadline (GA date, etc.): <!-- ENTER DATE HERE -->
- [ ] Other deadline: <!-- ENTER DATE HERE -->
- [x] None: Not urgent, can wait up to 1 week+

## SLA

- Teamwork makes the dream work, so please add a reviewer to your PRs.
- Please give the docs team up to 1 week to review your PR unless you've
added an urgent due date to it.
Thanks in advance for your help!

## 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)

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

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Johannes Daxböck <johannes.daxboeck@sentry.io>
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.

Support restrictedToMinimumLevel when configuring Serilog in code

2 participants