Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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>
91 changes: 91 additions & 0 deletions src/Sentry.NLog/SentryTarget.Structured.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
namespace Sentry.NLog;

public sealed partial class SentryTarget
{
private static void CaptureStructuredLog(IHub hub, SentryOptions options, LogEventInfo logEvent)
{
if (logEvent.Level.ToSentryLogLevel() is not { } level)
{
return;
}

DateTimeOffset timestamp = new(logEvent.TimeStamp);
GetStructuredLoggingParametersAndAttributes(logEvent, out var parameters, out var attributes);

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

var scope = hub.GetScope();
log.SetDefaultAttributes(options, scope, Sdk);
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);
}

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 = [];

if (!logEvent.HasProperties)
{
return;
}

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)
{
// NLog allows passing unnamed holes (e.g. `{}`) - parameters with no names.
// To prevent a collision on the attribute key when multiple unnamed holes are present, we fall back to the
// positional index of the parameter in these cases (matching the behaviour of MEL).
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();
}
}
Loading
Loading