Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
96f2581
feat(logs): add `NLog` integration
Flash0ver May 1, 2026
de54732
chore: update NLog sample app
Flash0ver May 1, 2026
f558baf
Merge branch 'main' into feat/logs-nlog
Flash0ver May 18, 2026
07330e7
fix(nlog): read EnableLogs from Hub options instead of target options
jamescrosswell Jul 15, 2026
ac37cef
refactor: inline SentryLog.Create into SentryLog.cs
jamescrosswell Jul 15, 2026
7e572c9
test(nlog): simplify structured log attribute assertions
jamescrosswell Jul 15, 2026
e2f8cc4
fix(nlog): attach logger name to structured logs
jamescrosswell Jul 15, 2026
5f3c30c
fix(nlog): give unnamed template holes positional parameter names
jamescrosswell Jul 15, 2026
4a228d0
refactor(nlog): simplify HashSet capacity guard
jamescrosswell Jul 15, 2026
4716b5c
Merge remote-tracking branch 'origin/main' into feat/logs-nlog
jamescrosswell Jul 15, 2026
70e19bc
fix(nlog): pass Scope to SetDefaultAttributes on structured logs
jamescrosswell Jul 15, 2026
35afe6f
refactor(nlog): flatten CaptureStructuredLog with a guard clause
jamescrosswell Jul 15, 2026
2da7f13
refactor(nlog): flatten GetStructuredLoggingParametersAndAttributes
jamescrosswell Jul 15, 2026
2b8c331
.
jamescrosswell Jul 15, 2026
44a9552
Tweaked comments
jamescrosswell Jul 15, 2026
9775861
fix(nlog): capture structured logs after events, guarded against fail…
jamescrosswell Jul 16, 2026
842e133
Merge remote-tracking branch 'origin/feat/logs-nlog' into feat/logs-nlog
jamescrosswell Jul 16, 2026
b0fea90
Refactored InnerWrite - was getting a bit long
jamescrosswell Jul 16, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion samples/Sentry.Samples.NLog/NLog.config
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
ignoreEventsWithNoException="False"
includeEventDataOnBreadcrumbs="False"
includeEventPropertiesAsTags="True"
minimumEventLevel="Error">
minimumEventLevel="Error"
enableLogs="True">

<!-- Advanced options can be configured here-->
<options
Expand Down
1 change: 1 addition & 0 deletions samples/Sentry.Samples.NLog/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ private static void UsingCodeConfiguration()
options.MinimumEventLevel = LogLevel.Error; // Error and higher is sent as event (default is Error)

options.AttachStacktrace = true;
options.EnableLogs = true; // send structured logs to Sentry
options.SendDefaultPii = true; // Send Personal Identifiable information like the username of the user logged in to the device

options.IncludeEventDataOnBreadcrumbs = true; // Optionally include event properties with breadcrumbs
Expand Down
15 changes: 15 additions & 0 deletions src/Sentry.NLog/LogLevelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,19 @@ public static BreadcrumbLevel ToBreadcrumbLevel(this LogLevel level)
_ => BreadcrumbLevel.Info
};
}

public static SentryLogLevel? ToSentryLogLevel(this LogLevel level)
{
return level.Name switch
{
nameof(LogLevel.Trace) => SentryLogLevel.Trace,
nameof(LogLevel.Debug) => SentryLogLevel.Debug,
nameof(LogLevel.Info) => SentryLogLevel.Info,
nameof(LogLevel.Warn) => SentryLogLevel.Warning,
nameof(LogLevel.Error) => SentryLogLevel.Error,
nameof(LogLevel.Fatal) => SentryLogLevel.Fatal,
nameof(LogLevel.Off) => null,
_ => null,
};
}
}
6 changes: 6 additions & 0 deletions src/Sentry.NLog/Sentry.NLog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@
<InternalsVisibleTo Include="Sentry.NLog.Tests" PublicKey="$(SentryPublicKey)" />
</ItemGroup>

<ItemGroup>
<Compile Update="SentryTarget.Structured.cs">
<DependentUpon>SentryTarget.cs</DependentUpon>
</Compile>
</ItemGroup>

</Project>
86 changes: 86 additions & 0 deletions src/Sentry.NLog/SentryTarget.Structured.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
namespace Sentry.NLog;

public sealed partial class SentryTarget
{
private static void CaptureStructuredLog(IHub hub, SentryOptions options, LogEventInfo logEvent)
{
var level = logEvent.Level.ToSentryLogLevel();
if (level.HasValue)
{
DateTimeOffset timestamp = new(logEvent.TimeStamp);
Comment thread
jamescrosswell marked this conversation as resolved.
Outdated
GetStructuredLoggingParametersAndAttributes(logEvent, out var parameters, out var attributes);

var log = SentryLog.Create(hub, timestamp, level.Value, logEvent.FormattedMessage, logEvent.Message, parameters);

log.SetDefaultAttributes(options, Sdk);

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-musl-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-musl-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-musl-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-musl-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-musl-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-musl-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-musl-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-musl-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-musl-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-musl-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / Analyze

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / Analyze

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / Analyze

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / Analyze

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / Analyze

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / Analyze

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / Analyze

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / Analyze

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / Analyze

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / Analyze

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (linux-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View check run for this annotation

@sentry/warden / warden: find-bugs

[YAU-G66] NLog CaptureStructuredLog passes SdkVersion to the Scope parameter of SetDefaultAttributes (additional location)

In `SentryTarget.Structured.cs`, `CaptureStructuredLog` calls `log.SetDefaultAttributes(options, Sdk)`. The `Sdk` field is a `SdkVersion`, but the target method signature is `SetDefaultAttributes(SentryOptions options, Scope? scope, SdkVersion? sdk = null)`, so `Sdk` is being bound to the `Scope? scope` parameter. `SdkVersion` is a sealed class with no implicit conversion to `Scope`, so this is a type mismatch. Even if it were to resolve, the `scope` argument would be wrong and the actual SDK version would never be passed, meaning scope-derived attributes (user.id, user.name, user.email, scope Sdk) are never applied to structured logs. Compare the correct Serilog implementation in `SentrySink.Structured.cs`: `var scope = hub.GetScope(); log.SetDefaultAttributes(options, scope, Sdk);`. Fix: obtain the scope from the hub (`var scope = hub.GetScope();`) and call `log.SetDefaultAttributes(options, scope, Sdk);`.

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-arm64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (win-x64)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / MSBuild

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / MSBuild

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / MSBuild

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / MSBuild

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / MSBuild

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (macos)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (macos)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (macos)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (macos)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'

Check failure on line 15 in src/Sentry.NLog/SentryTarget.Structured.cs

View workflow job for this annotation

GitHub Actions / .NET (macos)

Argument 2: cannot convert from 'Sentry.SdkVersion' to 'Sentry.Scope?'
log.SetOrigin("auto.log.nlog");

if (logEvent.LoggerName is not null)
{
log.Attributes.SetAttribute("category.name", logEvent.LoggerName);
}

foreach (var attribute in attributes)
{
log.SetAttribute(attribute.Key, attribute.Value);
}

hub.Logger.CaptureLog(log);
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated
}
}

private static void GetStructuredLoggingParametersAndAttributes(LogEventInfo logEvent, out ImmutableArray<KeyValuePair<string, object>> parameters, out List<KeyValuePair<string, object>> attributes)
{
parameters = GetParameters(logEvent, out var parameterNames);
attributes = new List<KeyValuePair<string, object>>();

if (logEvent.HasProperties)
{
foreach (var property in logEvent.Properties)
{
if (property.Key is string key && !string.IsNullOrWhiteSpace(key) &&
property.Value is { } value &&
!parameterNames.Contains(key))
{
attributes.Add(new KeyValuePair<string, object>($"property.{key}", value));
}
}
}
}

private static ImmutableArray<KeyValuePair<string, object>> GetParameters(LogEventInfo logEvent, out HashSet<string> parameterNames)
{
var parameters = logEvent.MessageTemplateParameters;

if (parameters.Count == 0)
{
parameterNames = new HashSet<string>();
return ImmutableArray<KeyValuePair<string, object>>.Empty;
}

// The HashSet<T> capacity constructor is unavailable on netstandard2.0 and net462 (added in net472).
#if NETSTANDARD2_0 || NET462
parameterNames = new HashSet<string>();
#else
parameterNames = new HashSet<string>(parameters.Count);
#endif

var @params = ImmutableArray.CreateBuilder<KeyValuePair<string, object>>(parameters.Count);

var index = 0;
foreach (var parameter in parameters)
{
// Unnamed holes (e.g. `{}`) have an empty name. Fall back to the positional index so that
// multiple unnamed holes don't collide on the same `sentry.message.parameter.` attribute key.
var name = string.IsNullOrEmpty(parameter.Name)
? index.ToString(CultureInfo.InvariantCulture)
: parameter.Name;

parameterNames.Add(name);
@params.Add(new KeyValuePair<string, object>(name, parameter.Value));
index++;
}
Comment thread
cursor[bot] marked this conversation as resolved.

return @params.DrainToImmutable();
}
}
26 changes: 25 additions & 1 deletion src/Sentry.NLog/SentryTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/// Sentry NLog Target.
/// </summary>
[Target("Sentry")]
public sealed class SentryTarget : TargetWithContext
public sealed partial class SentryTarget : TargetWithContext
{
// For testing:
internal Func<IHub> HubAccessor { get; }
Expand All @@ -14,6 +14,12 @@

internal static readonly SdkVersion NameAndVersion = typeof(SentryTarget).Assembly.GetNameAndVersion();

private static readonly SdkVersion Sdk = new()
{
Name = Constants.SdkName,
Version = NameAndVersion.Version,
};

internal static readonly string AdditionalGroupingKeyProperty = "AdditionalGroupingKey";

private static readonly string ProtocolPackageName = "nuget:" + NameAndVersion.Name;
Expand Down Expand Up @@ -129,6 +135,15 @@
set => Options.MinimumBreadcrumbLevel = LogLevel.FromString(value);
}

/// <summary>
/// Controls whether logs are generated and sent.
/// </summary>
public bool EnableLogs
{
get => Options.EnableLogs;
set => Options.EnableLogs = value;
}

/// <summary>
/// Whether the NLog integration should initialize the SDK.
/// </summary>
Expand Down Expand Up @@ -331,6 +346,15 @@
var shouldOnlyLogExceptions = exception == null && IgnoreEventsWithNoException;
var shouldIncludeProperties = ContextProperties?.Count > 0 || ShouldIncludeProperties(logEvent);

// Read the options from the Hub, rather than the Target's NLog-Options, because 'EnableLogs' is declared in the base 'SentryOptions', rather than the derived 'SentryNLogOptions'.
// In cases where Sentry's NLog-Target 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 Target's NLog-Options is default, but the Hub's Sentry-Options have the actual user-defined value configured.
var sentryOptions = hub.GetSentryOptions();
if (sentryOptions?.EnableLogs is true)
{

Check warning on line 354 in src/Sentry.NLog/SentryTarget.cs

View check run for this annotation

@sentry/warden / warden: code-review

NLog CaptureStructuredLog passes SdkVersion where SetDefaultAttributes expects Scope

In `SentryTarget.Structured.cs`, `CaptureStructuredLog` calls `log.SetDefaultAttributes(options, Sdk)`, passing the static `SdkVersion Sdk` field as the second argument. However, `SentryLog.SetDefaultAttributes` has the signature `(SentryOptions options, Scope? scope, SdkVersion? sdk = null)`, so the second positional argument must be a `Scope?`. There is no implicit conversion from `SdkVersion` to `Scope`, so this is a type mismatch. The other structured-logging integrations retrieve and pass the scope explicitly (Serilog: `log.SetDefaultAttributes(options, scope, Sdk)`; Extensions.Logging: `log.SetDefaultAttributes(_options, scope, _sdk)`). Because the NLog path never obtains a `Scope` (no `hub.GetScope()` call), scope-derived attributes set by `SetDefaultAttributes` — `user.id`, `user.name`, `user.email`, and the PII/`server.address` fallback — cannot be populated on structured logs, and the SdkVersion is passed into the wrong parameter.
Comment thread
sentry-warden[bot] marked this conversation as resolved.
CaptureStructuredLog(hub, sentryOptions, logEvent);

Check failure on line 355 in src/Sentry.NLog/SentryTarget.cs

View check run for this annotation

@sentry/warden / warden: find-bugs

NLog CaptureStructuredLog passes SdkVersion to the Scope parameter of SetDefaultAttributes

In `SentryTarget.Structured.cs`, `CaptureStructuredLog` calls `log.SetDefaultAttributes(options, Sdk)`. The `Sdk` field is a `SdkVersion`, but the target method signature is `SetDefaultAttributes(SentryOptions options, Scope? scope, SdkVersion? sdk = null)`, so `Sdk` is being bound to the `Scope? scope` parameter. `SdkVersion` is a sealed class with no implicit conversion to `Scope`, so this is a type mismatch. Even if it were to resolve, the `scope` argument would be wrong and the actual SDK version would never be passed, meaning scope-derived attributes (user.id, user.name, user.email, scope Sdk) are never applied to structured logs. Compare the correct Serilog implementation in `SentrySink.Structured.cs`: `var scope = hub.GetScope(); log.SetDefaultAttributes(options, scope, Sdk);`. Fix: obtain the scope from the hub (`var scope = hub.GetScope();`) and call `log.SetDefaultAttributes(options, scope, Sdk);`.
}
Comment thread
cursor[bot] marked this conversation as resolved.
Comment thread
cursor[bot] marked this conversation as resolved.

if (logEvent.Level >= Options.MinimumEventLevel && !shouldOnlyLogExceptions)
{
var formatted = RenderLogEvent(Layout, logEvent);
Expand Down
16 changes: 15 additions & 1 deletion src/Sentry/SentryLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Sentry;
/// Sentry .NET SDK Docs: <see href="https://docs.sentry.io/platforms/dotnet/logs/"/>.
/// </remarks>
[DebuggerDisplay(@"SentryLog \{ Level = {Level}, Message = '{Message}' \}")]
public sealed class SentryLog
public sealed partial class SentryLog
{
[SetsRequiredMembers]
internal SentryLog(DateTimeOffset timestamp, SentryId traceId, SentryLogLevel level, string message)
Expand All @@ -27,6 +27,20 @@ internal SentryLog(DateTimeOffset timestamp, SentryId traceId, SentryLogLevel le
Parameters = ImmutableArray<KeyValuePair<string, object>>.Empty;
}

internal static SentryLog Create(IHub hub, DateTimeOffset timestamp, SentryLogLevel level, string message, string? template, ImmutableArray<KeyValuePair<string, object>> parameters)
{
hub.GetTraceIdAndSpanId(out var traceId, out var spanId);

SentryLog log = new(timestamp, traceId, level, message)
{
Template = template,
Parameters = parameters,
SpanId = spanId,
};

return log;
}

/// <summary>
/// The timestamp of the log.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace Sentry.NLog
public NLog.Layouts.Layout? BreadcrumbCategory { get; set; }
public NLog.Layouts.Layout? BreadcrumbLayout { get; set; }
public NLog.Layouts.Layout? Dsn { get; set; }
public bool EnableLogs { get; set; }
public NLog.Layouts.Layout? Environment { get; set; }
public int FlushTimeoutSeconds { get; set; }
public bool IgnoreEventsWithNoException { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace Sentry.NLog
public NLog.Layouts.Layout? BreadcrumbCategory { get; set; }
public NLog.Layouts.Layout? BreadcrumbLayout { get; set; }
public NLog.Layouts.Layout? Dsn { get; set; }
public bool EnableLogs { get; set; }
public NLog.Layouts.Layout? Environment { get; set; }
public int FlushTimeoutSeconds { get; set; }
public bool IgnoreEventsWithNoException { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace Sentry.NLog
public NLog.Layouts.Layout? BreadcrumbCategory { get; set; }
public NLog.Layouts.Layout? BreadcrumbLayout { get; set; }
public NLog.Layouts.Layout? Dsn { get; set; }
public bool EnableLogs { get; set; }
public NLog.Layouts.Layout? Environment { get; set; }
public int FlushTimeoutSeconds { get; set; }
public bool IgnoreEventsWithNoException { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace Sentry.NLog
public NLog.Layouts.Layout? BreadcrumbCategory { get; set; }
public NLog.Layouts.Layout? BreadcrumbLayout { get; set; }
public NLog.Layouts.Layout? Dsn { get; set; }
public bool EnableLogs { get; set; }
public NLog.Layouts.Layout? Environment { get; set; }
public int FlushTimeoutSeconds { get; set; }
public bool IgnoreEventsWithNoException { get; set; }
Expand Down
6 changes: 6 additions & 0 deletions test/Sentry.NLog.Tests/Sentry.NLog.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@

</ItemGroup>

<ItemGroup>
<Compile Update="SentryTargetTests.Structured.cs">
<DependentUpon>SentryTargetTests.cs</DependentUpon>
</Compile>
</ItemGroup>

</Project>
Loading
Loading