Skip to content

feat(logs): add NLog integration#5176

Open
Flash0ver wants to merge 3 commits into
mainfrom
feat/logs-nlog
Open

feat(logs): add NLog integration#5176
Flash0ver wants to merge 3 commits into
mainfrom
feat/logs-nlog

Conversation

@Flash0ver

Copy link
Copy Markdown
Member

closes #5167

Changes

Add NLog integration to Sentry Structured Logging.

Docs

  • TODO

@Flash0ver Flash0ver self-assigned this May 1, 2026
@codecov

codecov Bot commented May 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.91525% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.19%. Comparing base (7deff34) to head (f558baf).
⚠️ Report is 21 commits behind head on main.

Files with missing lines Patch % Lines
src/Sentry.NLog/LogLevelExtensions.cs 72.72% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5176      +/-   ##
==========================================
+ Coverage   74.13%   74.19%   +0.06%     
==========================================
  Files         508      510       +2     
  Lines       18282    18341      +59     
  Branches     3574     3583       +9     
==========================================
+ Hits        13553    13609      +56     
- Misses       3860     3862       +2     
- Partials      869      870       +1     

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

@Flash0ver Flash0ver marked this pull request as ready for review May 18, 2026 11:23
@Flash0ver Flash0ver requested a review from jamescrosswell as a code owner May 18, 2026 11:23
Comment on lines +96 to +105
log.TryGetAttribute("sentry.environment", out object? environment).Should().BeTrue();
environment.Should().Be("test-environment");
log.TryGetAttribute("sentry.release", out object? release).Should().BeTrue();
release.Should().Be("test-release");
log.TryGetAttribute("sentry.origin", out object? origin).Should().BeTrue();
origin.Should().Be("auto.log.nlog");
log.TryGetAttribute("sentry.sdk.name", out object? sdkName).Should().BeTrue();
sdkName.Should().Be(Constants.SdkName);
log.TryGetAttribute("sentry.sdk.version", out object? sdkVersion).Should().BeTrue();
sdkVersion.Should().Be(SentryTarget.NameAndVersion.Version);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

suggestion: reference project Sentry.Testing and simplify assertions with extensions added in #4936

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

Reviewed by Cursor Bugbot for commit f558baf. Configure here.

if (Options.EnableLogs)
{
CaptureStructuredLog(hub, Options, logEvent);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

NLog reads EnableLogs from wrong options source

High Severity

The NLog integration checks Options.EnableLogs (the target's own SentryNLogOptions) and passes Options to CaptureStructuredLog, but the Serilog integration explicitly reads from hub.GetSentryOptions() instead, with a detailed comment explaining why. When the SDK is initialized elsewhere (e.g., ASP.NET Core) and InitializeSdk is false, the NLog target's own Options.EnableLogs remains the default false, so structured logs silently won't be sent even though the user enabled them on the actual SDK options. The options passed to SetDefaultAttributes may also carry incorrect Environment/Release values.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit f558baf. Configure here.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This checks out - I think the same reasoning applies here:

// Read the options from the Hub, rather than the Sink's Serilog-Options, because 'EnableLogs' is declared in the base 'SentryOptions', rather than the derived 'SentrySerilogOptions'.
// In cases where Sentry's Serilog-Sink is added without a DSN (i.e., without initializing the SDK) and the SDK is initialized differently (e.g., through ASP.NET Core),
// then the 'EnableLogs' option of this Sink's Serilog-Options is default, but the Hub's Sentry-Options have the actual user-defined value configured.
var options = hub.GetSentryOptions();
if (options?.EnableLogs is true)
{
CaptureStructuredLog(hub, options, logEvent, formatted, template);
}

NB: More evidence to support #5245

return ImmutableArray<KeyValuePair<string, object>>.Empty;
}

#if NETSTANDARD2_1_OR_GREATER || NET472_OR_GREATER || NETCOREAPP2_0_OR_GREATER

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could potentially simplify this. I think the only scenario where we don't take this codepath is if the TFM is netstandard2.0 right?

@jamescrosswell jamescrosswell Jun 23, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not a big fan of splitting a single method out into a separate file (and the extra overhead that adds to the csproj file). My preference would be to simply add it to SentryLog.cs... the method name itself lets you know it's a factory method.

@jamescrosswell jamescrosswell left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Overall looks pretty good. No blocking comments from me - mostly stylistic.

The main one that needs to be dealt with is the one that the Sentry bot called out since that would be a bug for certain initialisation paths.

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.

Add Sentry Logs support to NLog

2 participants