Skip to content

Commit a56c634

Browse files
docs: add an ASP.NET Core NLog sample and stop advising AddSentry() when another integration handles ILogger (#5821)
* docs: add an ASP.NET Core NLog sample and stop advising AddSentry() when another integration handles ILogger The sample routes ILogger through NLog with ReplaceLoggerFactory, so Sentry's own ILogger providers stand aside and each entry reaches Sentry once. The ClearProviders() warning and the AddSentry() remarks told apps to add Sentry's providers back, which sends every entry twice when NLog, Serilog or log4net already forward ILogger entries to Sentry. Refs #5763 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: show breadcrumb, event and structured log levels in the ASP.NET Core NLog sample Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: simplify the Logging.AddSentry() remarks Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Apply suggestion from @jamescrosswell --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6c879cc commit a56c634

14 files changed

Lines changed: 103 additions & 11 deletions

‎.generated.NoMobile.slnx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<Project Path="samples/Sentry.Samples.AspNetCore.Blazor.Wasm/Sentry.Samples.AspNetCore.Blazor.Wasm.csproj" />
9090
<Project Path="samples/Sentry.Samples.AspNetCore.Grpc/Sentry.Samples.AspNetCore.Grpc.csproj" />
9191
<Project Path="samples/Sentry.Samples.AspNetCore.Mvc/Sentry.Samples.AspNetCore.Mvc.csproj" />
92+
<Project Path="samples/Sentry.Samples.AspNetCore.NLog/Sentry.Samples.AspNetCore.NLog.csproj" />
9293
<Project Path="samples/Sentry.Samples.AspNetCore.Serilog/Sentry.Samples.AspNetCore.Serilog.csproj" />
9394
<Project Path="samples/Sentry.Samples.AspNetCore.WebAPI.Profiling/Sentry.Samples.AspNetCore.WebAPI.Profiling.csproj" />
9495
<Project Path="samples/Sentry.Samples.Aws.Lambda.AspNetCoreServer/Sentry.Samples.Aws.Lambda.AspNetCoreServer.csproj" />

‎Sentry-CI-Build-Linux-NoMobile.slnf‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"samples\\Sentry.Samples.AspNetCore.Blazor.Wasm\\Sentry.Samples.AspNetCore.Blazor.Wasm.csproj",
1010
"samples\\Sentry.Samples.AspNetCore.Grpc\\Sentry.Samples.AspNetCore.Grpc.csproj",
1111
"samples\\Sentry.Samples.AspNetCore.Mvc\\Sentry.Samples.AspNetCore.Mvc.csproj",
12+
"samples\\Sentry.Samples.AspNetCore.NLog\\Sentry.Samples.AspNetCore.NLog.csproj",
1213
"samples\\Sentry.Samples.AspNetCore.Serilog\\Sentry.Samples.AspNetCore.Serilog.csproj",
1314
"samples\\Sentry.Samples.AspNetCore.WebAPI.Profiling\\Sentry.Samples.AspNetCore.WebAPI.Profiling.csproj",
1415
"samples\\Sentry.Samples.Aws.Lambda.AspNetCoreServer\\Sentry.Samples.Aws.Lambda.AspNetCoreServer.csproj",

‎Sentry-CI-Build-Linux.slnf‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"samples\\Sentry.Samples.AspNetCore.Blazor.Wasm\\Sentry.Samples.AspNetCore.Blazor.Wasm.csproj",
1111
"samples\\Sentry.Samples.AspNetCore.Grpc\\Sentry.Samples.AspNetCore.Grpc.csproj",
1212
"samples\\Sentry.Samples.AspNetCore.Mvc\\Sentry.Samples.AspNetCore.Mvc.csproj",
13+
"samples\\Sentry.Samples.AspNetCore.NLog\\Sentry.Samples.AspNetCore.NLog.csproj",
1314
"samples\\Sentry.Samples.AspNetCore.Serilog\\Sentry.Samples.AspNetCore.Serilog.csproj",
1415
"samples\\Sentry.Samples.AspNetCore.WebAPI.Profiling\\Sentry.Samples.AspNetCore.WebAPI.Profiling.csproj",
1516
"samples\\Sentry.Samples.Aws.Lambda.AspNetCoreServer\\Sentry.Samples.Aws.Lambda.AspNetCoreServer.csproj",

‎Sentry-CI-Build-Windows-arm64.slnf‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"samples\\Sentry.Samples.AspNetCore.Blazor.Wasm\\Sentry.Samples.AspNetCore.Blazor.Wasm.csproj",
1212
"samples\\Sentry.Samples.AspNetCore.Grpc\\Sentry.Samples.AspNetCore.Grpc.csproj",
1313
"samples\\Sentry.Samples.AspNetCore.Mvc\\Sentry.Samples.AspNetCore.Mvc.csproj",
14+
"samples\\Sentry.Samples.AspNetCore.NLog\\Sentry.Samples.AspNetCore.NLog.csproj",
1415
"samples\\Sentry.Samples.AspNetCore.Serilog\\Sentry.Samples.AspNetCore.Serilog.csproj",
1516
"samples\\Sentry.Samples.AspNetCore.WebAPI.Profiling\\Sentry.Samples.AspNetCore.WebAPI.Profiling.csproj",
1617
"samples\\Sentry.Samples.Aws.Lambda.AspNetCoreServer\\Sentry.Samples.Aws.Lambda.AspNetCoreServer.csproj",

‎Sentry-CI-Build-Windows.slnf‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"samples\\Sentry.Samples.AspNetCore.Blazor.Wasm\\Sentry.Samples.AspNetCore.Blazor.Wasm.csproj",
1212
"samples\\Sentry.Samples.AspNetCore.Grpc\\Sentry.Samples.AspNetCore.Grpc.csproj",
1313
"samples\\Sentry.Samples.AspNetCore.Mvc\\Sentry.Samples.AspNetCore.Mvc.csproj",
14+
"samples\\Sentry.Samples.AspNetCore.NLog\\Sentry.Samples.AspNetCore.NLog.csproj",
1415
"samples\\Sentry.Samples.AspNetCore.Serilog\\Sentry.Samples.AspNetCore.Serilog.csproj",
1516
"samples\\Sentry.Samples.AspNetCore.WebAPI.Profiling\\Sentry.Samples.AspNetCore.WebAPI.Profiling.csproj",
1617
"samples\\Sentry.Samples.Aws.Lambda.AspNetCoreServer\\Sentry.Samples.Aws.Lambda.AspNetCoreServer.csproj",

‎Sentry-CI-Build-macOS.slnf‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"samples\\Sentry.Samples.AspNetCore.Blazor.Wasm\\Sentry.Samples.AspNetCore.Blazor.Wasm.csproj",
1313
"samples\\Sentry.Samples.AspNetCore.Grpc\\Sentry.Samples.AspNetCore.Grpc.csproj",
1414
"samples\\Sentry.Samples.AspNetCore.Mvc\\Sentry.Samples.AspNetCore.Mvc.csproj",
15+
"samples\\Sentry.Samples.AspNetCore.NLog\\Sentry.Samples.AspNetCore.NLog.csproj",
1516
"samples\\Sentry.Samples.AspNetCore.Serilog\\Sentry.Samples.AspNetCore.Serilog.csproj",
1617
"samples\\Sentry.Samples.AspNetCore.WebAPI.Profiling\\Sentry.Samples.AspNetCore.WebAPI.Profiling.csproj",
1718
"samples\\Sentry.Samples.Aws.Lambda.AspNetCoreServer\\Sentry.Samples.Aws.Lambda.AspNetCoreServer.csproj",

‎Sentry.slnx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<Project Path="samples/Sentry.Samples.AspNetCore.Blazor.Wasm/Sentry.Samples.AspNetCore.Blazor.Wasm.csproj" />
9090
<Project Path="samples/Sentry.Samples.AspNetCore.Grpc/Sentry.Samples.AspNetCore.Grpc.csproj" />
9191
<Project Path="samples/Sentry.Samples.AspNetCore.Mvc/Sentry.Samples.AspNetCore.Mvc.csproj" />
92+
<Project Path="samples/Sentry.Samples.AspNetCore.NLog/Sentry.Samples.AspNetCore.NLog.csproj" />
9293
<Project Path="samples/Sentry.Samples.AspNetCore.Serilog/Sentry.Samples.AspNetCore.Serilog.csproj" />
9394
<Project Path="samples/Sentry.Samples.AspNetCore.WebAPI.Profiling/Sentry.Samples.AspNetCore.WebAPI.Profiling.csproj" />
9495
<Project Path="samples/Sentry.Samples.Aws.Lambda.AspNetCoreServer/Sentry.Samples.Aws.Lambda.AspNetCoreServer.csproj" />

‎SentryAspNetCore.slnf‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"samples\\Sentry.Samples.AspNetCore.Blazor.Wasm\\Sentry.Samples.AspNetCore.Blazor.Wasm.csproj",
88
"samples\\Sentry.Samples.AspNetCore.Grpc\\Sentry.Samples.AspNetCore.Grpc.csproj",
99
"samples\\Sentry.Samples.AspNetCore.Mvc\\Sentry.Samples.AspNetCore.Mvc.csproj",
10+
"samples\\Sentry.Samples.AspNetCore.NLog\\Sentry.Samples.AspNetCore.NLog.csproj",
1011
"samples\\Sentry.Samples.AspNetCore.Serilog\\Sentry.Samples.AspNetCore.Serilog.csproj",
1112
"samples\\Sentry.Samples.AspNetCore.WebAPI.Profiling\\Sentry.Samples.AspNetCore.WebAPI.Profiling.csproj",
1213
"samples\\Sentry.Samples.Aws.Lambda.AspNetCoreServer\\Sentry.Samples.Aws.Lambda.AspNetCoreServer.csproj",

‎SentryNoMobile.slnf‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"samples\\Sentry.Samples.AspNetCore.Blazor.Wasm\\Sentry.Samples.AspNetCore.Blazor.Wasm.csproj",
1010
"samples\\Sentry.Samples.AspNetCore.Grpc\\Sentry.Samples.AspNetCore.Grpc.csproj",
1111
"samples\\Sentry.Samples.AspNetCore.Mvc\\Sentry.Samples.AspNetCore.Mvc.csproj",
12+
"samples\\Sentry.Samples.AspNetCore.NLog\\Sentry.Samples.AspNetCore.NLog.csproj",
1213
"samples\\Sentry.Samples.AspNetCore.Serilog\\Sentry.Samples.AspNetCore.Serilog.csproj",
1314
"samples\\Sentry.Samples.AspNetCore.WebAPI.Profiling\\Sentry.Samples.AspNetCore.WebAPI.Profiling.csproj",
1415
"samples\\Sentry.Samples.Aws.Lambda.AspNetCoreServer\\Sentry.Samples.Aws.Lambda.AspNetCoreServer.csproj",
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using NLog;
2+
using NLog.Config;
3+
using NLog.Targets;
4+
using NLog.Web;
5+
using Sentry.NLog;
6+
7+
// Configure NLog to send logs to Sentry. This only adds the Sentry target: the SDK is initialized by UseSentry below.
8+
var sentryTarget = new SentryTarget(new SentryNLogOptions
9+
{
10+
// Warn and higher are added as breadcrumbs (default is Info)
11+
MinimumBreadcrumbLevel = NLog.LogLevel.Warn,
12+
// Error and higher are sent as events (default is Error)
13+
MinimumEventLevel = NLog.LogLevel.Error,
14+
});
15+
16+
var nlogConfig = new LoggingConfiguration();
17+
nlogConfig.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, new ColoredConsoleTarget("console"));
18+
// Every entry that reaches the Sentry target is sent as a structured log, so this rule sets the minimum level for logs.
19+
// Info and higher are sent as logs. Breadcrumbs and events can only use entries that pass this rule.
20+
nlogConfig.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, sentryTarget);
21+
LogManager.Configuration = nlogConfig;
22+
23+
var builder = WebApplication.CreateBuilder(args);
24+
25+
// Send ILogger entries through NLog. ReplaceLoggerFactory stops the ILogger providers that UseSentry adds from also
26+
// capturing every entry, so each one reaches Sentry once, through the NLog target, whatever order these calls are in.
27+
builder.Host.UseNLog(new NLogAspNetCoreOptions { ReplaceLoggerFactory = true });
28+
29+
builder.UseSentry(options =>
30+
{
31+
#if !SENTRY_DSN_DEFINED_IN_ENV
32+
// A DSN is required. You can set here in code, or you can set it in the SENTRY_DSN environment variable.
33+
// See https://docs.sentry.io/product/sentry-basics/dsn-explainer/
34+
options.Dsn = SamplesShared.Dsn;
35+
#else
36+
options.Dsn = EnvironmentVariables.Dsn;
37+
#endif
38+
});
39+
40+
var app = builder.Build();
41+
42+
app.MapGet("/", (ILogger<Program> logger) =>
43+
{
44+
logger.LogDebug("Below the Sentry rule's minimum level, so not sent to Sentry");
45+
logger.LogInformation("Sent as a log");
46+
logger.LogWarning("Sent as a log and added as a breadcrumb");
47+
logger.LogError("Sent as a log and as an event, which includes the breadcrumb above");
48+
return "Logged through ILogger at each level. Check Sentry for the logs and the event.";
49+
});
50+
51+
app.MapGet("/throw", () => { throw new Exception("An exception thrown from the ASP.NET Core pipeline"); });
52+
53+
app.Run();
54+
55+
LogManager.Shutdown();

0 commit comments

Comments
 (0)