Skip to content

feat(nlog): add MinimumLogLevel so Trace and Debug entries aren't sent as logs by default - #5913

Merged
jamescrosswell merged 7 commits into
version7from
feat/nlog-addsentry-minlevel
Nov 11, 2026
Merged

jamescrosswell merged 7 commits into
version7from
feat/nlog-addsentry-minlevel

Conversation

@jamescrosswell

@jamescrosswell jamescrosswell commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

LoggingConfiguration.AddSentry() adds a rule for every level, and in 7.0 every entry that reaches the Sentry target is sent as a structured log. Apps that route ILogger through NLog (ReplaceLoggerFactory = true, as the 7.0 docs recommend alongside UseSentry) therefore sent every ASP.NET Core Trace and Debug entry to Sentry as a log.

Whatever the fix, it has to let logs be filtered separately from breadcrumbs and events. An app that wants Trace breadcrumbs but only Info and higher as logs should be able to have both.

How the other integrations do it

MEL is the ideal. Logs come from their own provider, SentryStructuredLoggerProvider, aliased Sentry. Breadcrumbs and events come from SentryLoggerProvider. So the standard Logging configuration filters logs on their own:

"Logging": { "Sentry": { "LogLevel": { "Default": "Information" } } }

NLog can't do that. A single SentryTarget sends events, breadcrumbs and logs, and NLog's rules (AddRule, minlevel, <filters>) decide what reaches the target as a whole. A rule can't let an entry through for breadcrumbs and hold it back from logs.

Serilog splits the job in two:

  • RestrictedToMinimumLevel gates everything that reaches the sink.
  • MinimumEventLevel and MinimumBreadcrumbLevel set thresholds for each kind of data.

What this PR does

It follows Serilog's approach, with NLog's rules as the gate:

  • The NLog rule plays the part of RestrictedToMinimumLevel. It decides what reaches the Sentry target at all, as it always has. AddSentry() now starts its rule at the lowest of the three minimum levels instead of adding one for every level, so entries the target would drop never reach it.
  • The target's thresholds decide what each entry becomes. SentryNLogOptions gets MinimumLogLevel, next to MinimumEventLevel and MinimumBreadcrumbLevel. It defaults to Info and only filters logs.

Serilog has no threshold for logs, so there the sink-wide gate is the only way to filter them. MinimumLogLevel gives NLog the separation MEL gets from its two providers:

config.AddSentry(o =>
{
    o.MinimumBreadcrumbLevel = NLog.LogLevel.Trace;
    o.MinimumLogLevel = NLog.LogLevel.Info; // the default
});

It's also a target attribute, minimumLogLevel="Debug", for XML config. With the Info default, the docs can go back to a plain config.AddSentry(...) (getsentry/sentry-docs#19925). The ASP.NET Core NLog sample now uses AddSentry instead of building the rule by hand.

Worth flagging in review

  • Behaviour change: Trace and Debug entries are no longer sent as logs unless MinimumLogLevel is lowered. This applies to XML-configured targets too.
  • enableLogs="false" now sets MinimumLogLevel to Off, so configs carried over from 6.x keep turning logs off. Before this PR it only wrote a warning. EnableLogs stays [Obsolete], pointing to MinimumLogLevel.
  • The public API change is additive (AddSentry's signature is unchanged), so the title has no !. Add it if the default change should be listed under Breaking Changes.

Closes #5909

🤖 Generated with Claude Code

AddSentry() added a rule for every level, and every entry reaching the
Sentry target is sent as a structured log. Apps that route ILogger through
NLog therefore sent every Trace and Debug entry as a log. The new optional
minLevel parameter sets the rule's lower bound; it defaults to Trace, so
existing behaviour is unchanged.

The ASP.NET Core NLog sample now uses AddSentry(minLevel: ...) instead of
building the rule by hand.

Closes #5909

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Trace and Debug entries no longer reach the Sentry target unless minLevel
says so, matching Microsoft.Extensions.Logging's usual Information default.
The console sample lowers MinimumBreadcrumbLevel to Debug, so it now passes
minLevel: Debug to keep those breadcrumbs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jamescrosswell jamescrosswell changed the title feat(nlog)!: add minLevel to AddSentry so entries below it aren't sent as logs feat(nlog)!: AddSentry only sends Info and higher by default, with a minLevel parameter to change it Nov 11, 2026
…mumLogLevel

A rule bound also limits breadcrumbs and events, so Trace breadcrumbs
couldn't be kept alongside Info-and-up logs. MinimumLogLevel sits next to
MinimumEventLevel and MinimumBreadcrumbLevel, filters only structured logs,
defaults to Info, and can be set from XML (minimumLogLevel="Debug").
AddSentry goes back to a rule for every level, with its 6.x signature.

The EnableLogs messages now point at MinimumLogLevel=Off rather than
SetBeforeSendLog.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jamescrosswell jamescrosswell changed the title feat(nlog)!: AddSentry only sends Info and higher by default, with a minLevel parameter to change it feat(nlog): add MinimumLogLevel so Trace and Debug entries aren't sent as logs by default Nov 11, 2026
@codecov

codecov Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 76.99%. Comparing base (25b505e) to head (9a93a52).
⚠️ Report is 3 commits behind head on version7.

Files with missing lines Patch % Lines
src/Sentry.NLog/SentryTarget.cs 80.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           version7    #5913      +/-   ##
============================================
+ Coverage     76.98%   76.99%   +0.01%     
============================================
  Files           538      538              
  Lines         19714    19719       +5     
  Branches       3872     3874       +2     
============================================
+ Hits          15176    15182       +6     
- Misses         3666     3667       +1     
+ Partials        872      870       -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.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jamescrosswell
jamescrosswell marked this pull request as ready for review November 11, 2026 07:58
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 11, 2026
Comment thread src/Sentry.NLog/SentryNLogOptions.cs Resolved
jamescrosswell and others added 2 commits November 11, 2026 22:00
AddSentry added a rule for every level, so entries below all three
minimum levels still reached the target and were dropped there. The rule
now starts at the lowest of MinimumEventLevel, MinimumBreadcrumbLevel and
MinimumLogLevel (Info by default).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Changing options after setup isn't supported, so it doesn't need documenting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/Sentry.NLog/SentryTarget.cs Resolved
In 6.x enableLogs="false" turned logs off. It was only a warning in 7.0
because the target had no way to turn logs off; MinimumLogLevel now
gives it one, so existing configs keep their meaning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jamescrosswell
jamescrosswell merged commit 84d6864 into version7 Nov 11, 2026
37 checks passed
@jamescrosswell
jamescrosswell deleted the feat/nlog-addsentry-minlevel branch November 11, 2026 21:06
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.

NLog: AddSentry() has no minimum level, so Trace and Debug entries routed through NLog are sent as logs

2 participants