Skip to content

Commit 5a82a09

Browse files
author
Josh DeGraw
committed
Added layout configs for breadcrumb & events to Options
Added ability to specify a separate layout for breadcrumbs, and added Layout and BreadcrumbLayout to SentryNLogOptions to allow them to be configured programmatically.
1 parent da6b036 commit 5a82a09

File tree

5 files changed

+60
-8
lines changed

5 files changed

+60
-8
lines changed

samples/Sentry.Samples.NLog/NLog.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<target name="logconsole" xsi:type="ColoredConsole" />
1111
<target xsi:type="Sentry" name="sentry"
1212
dsn="https://[email protected]/1188141"
13+
layout="${message}"
14+
breadcrumbLayout="${message}"
1315
minimumBreadcrumbLevel="Debug"
1416
minimumEventLevel="Error">
1517

samples/Sentry.Samples.NLog/Program.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ private static void Main(string[] args)
3232

3333
private static void DemoLogger(ILogger logger)
3434
{
35-
// Minimum Breadcrumb and Event log levels are set to levels higher than Verbose In this case,
36-
// Verbose messages are ignored
35+
// Minimum Breadcrumb and Event log levels are set to levels higher than Trace.
36+
// In this case, Trace messages are ignored
3737
logger.Trace("Verbose message which is not sent.");
3838

3939
// Minimum Breadcrumb level is set to Debug so the following message is stored in memory and sent
@@ -82,16 +82,20 @@ private static void UsingCodeConfiguration()
8282
config
8383
.AddSentry(o =>
8484
{
85-
o.MinimumBreadcrumbLevel = LogLevel.Debug; // Debug and higher are stored as breadcrumbs (default os Information)
85+
o.Layout = "${message}";
86+
o.BreadcrumbLayout = "${logger}: ${message}"; // Optionally specify a separate format for breadcrumbs
87+
88+
o.MinimumBreadcrumbLevel = LogLevel.Debug; // Debug and higher are stored as breadcrumbs (default is Info)
8689
o.MinimumEventLevel = LogLevel.Error; // Error and higher is sent as event (default is Error)
8790

8891
// If DSN is not set, the SDK will look for an environment variable called SENTRY_DSN. If
8992
// nothing is found, SDK is disabled.
9093
o.Dsn = new Dsn("https://[email protected]/1188141");
94+
9195
o.AttachStacktrace = true;
92-
o.SendDefaultPii = true; // send Personal Identifiable information like the username of the user logged in to the device
96+
o.SendDefaultPii = true; // Send Personal Identifiable information like the username of the user logged in to the device
9397

94-
o.IncludeEventDataOnBreadcrumbs = true;
98+
o.IncludeEventDataOnBreadcrumbs = true; // Optionally include event properties with breadcrumbs
9599
o.ShutdownTimeoutSeconds = 5;
96100

97101
o.AddTag("logger", "${logger}"); // Send the logger name as a tag
@@ -104,6 +108,7 @@ private static void UsingCodeConfiguration()
104108
config.AddTarget(new ColoredConsoleTarget("console"));
105109
config.AddRuleForAllLevels("console");
106110
config.AddRuleForAllLevels("debugger");
111+
107112
LogManager.Configuration = config;
108113

109114
var Log = LogManager.GetCurrentClassLogger();

src/Sentry.NLog/SentryNLogOptions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using NLog;
55
using NLog.Config;
6+
using NLog.Layouts;
67
using NLog.Targets;
78

89
namespace Sentry.NLog
@@ -56,6 +57,18 @@ public int ShutdownTimeoutSeconds
5657
/// </summary>
5758
public bool IncludeEventDataOnBreadcrumbs { get; set; } = false;
5859

60+
/// <summary>
61+
/// Custom layout for breadcrumbs.
62+
/// </summary>
63+
[NLogConfigurationIgnoreProperty] // Configure this directly on the target in XML config.
64+
public Layout BreadcrumbLayout { get; set; }
65+
66+
/// <summary>
67+
/// Configured layout for the NLog logger.
68+
/// </summary>
69+
[NLogConfigurationIgnoreProperty] // Configure this directly on the target in XML config.
70+
public Layout Layout { get; set; }
71+
5972
/// <summary>
6073
/// Any additional tags to apply to each logged message.
6174
/// </summary>

src/Sentry.NLog/SentryTarget.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using NLog;
77
using NLog.Config;
8+
using NLog.Layouts;
89
using NLog.Targets;
910

1011
using Sentry.Extensibility;
@@ -76,6 +77,15 @@ public string Dsn
7677
set => Options.Dsn = new Dsn(value);
7778
}
7879

80+
/// <summary>
81+
/// An optional layout specific to breadcrumbs. If not set, uses the same layout as the standard <see cref="TargetWithContext.Layout"/>.
82+
/// </summary>
83+
public Layout BreadcrumbLayout
84+
{
85+
get => Options.BreadcrumbLayout ?? Layout;
86+
set => Options.BreadcrumbLayout = value;
87+
}
88+
7989
/// <summary>
8090
/// Minimum log level for events to trigger a send to Sentry. Defaults to <see cref="M:LogLevel.Error" />.
8191
/// </summary>
@@ -108,6 +118,10 @@ protected override void InitializeTarget()
108118

109119
IncludeEventProperties = Options.SendEventPropertiesAsData;
110120

121+
// If a layout has been configured on the options, replace the default logger.
122+
if (Options.Layout != null)
123+
Layout = Options.Layout;
124+
111125
// If the sdk is not there, set it on up.
112126
if (Options.InitializeSdk && _sdkDisposable == null)
113127
{
@@ -179,9 +193,11 @@ protected override void Write(LogEventInfo logEvent)
179193
// Whether or not it was sent as event, add breadcrumb so the next event includes it
180194
if (logEvent.Level >= Options.MinimumBreadcrumbLevel)
181195
{
182-
var message = string.IsNullOrWhiteSpace(formatted)
196+
var breadcrumbFormatted = BreadcrumbLayout.Render(logEvent);
197+
198+
var message = string.IsNullOrWhiteSpace(breadcrumbFormatted)
183199
? exception?.Message ?? string.Empty
184-
: formatted;
200+
: breadcrumbFormatted;
185201

186202
IDictionary<string, string> data = null;
187203

test/Sentry.NLog.Tests/SentryTargetTests.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,22 @@ public void Log_SourceContextContainsSentry_NoScopeConfigured()
328328

329329
_fixture.Hub.DidNotReceive().ConfigureScope(Arg.Any<Action<BaseScope>>());
330330
}
331-
}
332331

332+
[Fact]
333+
public void Log_WithCustomBreadcrumbLayout_RendersCorrectly()
334+
{
335+
var logger = _fixture.GetLogger(o =>
336+
{
337+
o.MinimumBreadcrumbLevel = LogLevel.Trace;
338+
o.BreadcrumbLayout = "${logger}: ${message}";
339+
});
340+
const string message = "This is a breadcrumb";
341+
342+
logger.Debug(message);
343+
344+
var b = _fixture.Scope.Breadcrumbs.First();
345+
346+
Assert.Equal($"{logger.Name}: {message}", b.Message);
347+
}
348+
}
333349
}

0 commit comments

Comments
 (0)