Skip to content

Commit 9bfecaa

Browse files
ric-olivclaude
andauthored
docs(dotnet): Show how to turn off logs from Logging.AddSentry() (#20947)
## DESCRIBE YOUR PR Since 7.0.0, apps that call `SentrySdk.Init` and add `Logging.AddSentry()` send every `ILogger` entry to Sentry as a log. The migration guide only shows `SetBeforeSendLog(_ => null)` to turn that off. That callback still builds each log before it drops it, and it can't be set from configuration. getsentry/sentry-dotnet#5988 asks for a switch that can. That switch already exists. The logs provider that `Logging.AddSentry()` adds has the `Sentry` alias, so setting `Logging:Sentry:LogLevel:Default` to `None` turns off its logs and keeps its errors and breadcrumbs. The migration guide now shows it next to `SetBeforeSendLog` as a second option, in configuration and in code. The same alias is on the logs providers that `UseSentry` adds in ASP.NET Core and MAUI. The guide also names NLog's `minimumLogLevel` as that integration's switch, and says that the Serilog sink and the log4net appender have none yet (getsentry/sentry-dotnet#5995, getsentry/sentry-dotnet#5996), so they need the callback. The Logs page already named the alias. It now also says that a `LogLevel` section under `Logging:Sentry` applies only to Sentry's logs, and that its keys are categories. I ran the snippets against `Sentry.Extensions.Logging` 7.0.0. With `None`, an info, a warning and an error still produced three breadcrumbs and one event, and no logs. A per-category level under `Logging:Sentry` changed only which entries became logs and left breadcrumbs unchanged. Refs getsentry/sentry-dotnet#5988 ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. Select exactly one option. For deadlines, replace `YYYY-MM-DD` with the due date. You can update this information later by editing the PR description. - [ ] Urgent deadline (GA date, etc.): YYYY-MM-DD - [ ] Other deadline: YYYY-MM-DD - [x] No deadline: Not urgent, can wait up to 1 week+ ## PRE-MERGE CHECKLIST _Make sure you've checked the following before merging your changes:_ - [x] Checked Vercel preview for correctness, including links - [x] PR was reviewed and approved by any necessary SMEs (subject matter experts) - [ ] PR was reviewed and approved by a member of the [Sentry docs team](https://github.com/orgs/getsentry/teams/docs) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0bcc054 commit 9bfecaa

3 files changed

Lines changed: 37 additions & 5 deletions

File tree

‎includes/migration/dotnet-v7/enablelogs-is-removed-from-sentryoptions.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Version 7 removes `EnableLogs` from `SentryOptions`, the core options of the SDK
1313

1414
Whether logs are on now depends on how your app writes them:
1515

16-
- **Logging integrations.** If you add the Serilog, NLog or log4net integration, or call `Logging.AddSentry()` after `SentrySdk.Init`, logs are on by default. Your logging framework's configuration, such as its minimum level, controls which entries they send. See [Logging Integrations Send Logs](#logging-integrations-send-logs).
16+
- **Logging integrations.** If you add the Serilog, NLog or log4net integration, or call `Logging.AddSentry()` after `SentrySdk.Init`, logs are on by default. Your logging framework's configuration, such as its minimum level, controls which entries they send. To turn off the logs from `Logging.AddSentry()` and keep its errors and breadcrumbs, set `Logging:Sentry:LogLevel:Default` to `None`. See [Logging Integrations Send Logs](#logging-integrations-send-logs).
1717
- **Host integrations.** In ASP.NET Core, MAUI, Blazor WebAssembly and generic host apps, `UseSentry` adds an `ILogger` integration. It sends errors and breadcrumbs, but its logs are off by default. To turn them on, set `EnableLogs = true` on the options of `UseSentry`. `EnableLogs` doesn't affect a Serilog, NLog or log4net integration in the same app.
1818
- **`SentrySdk.Logger`.** It sends logs in every app and needs no option.
1919

‎includes/migration/dotnet-v7/logging-integrations-send-logs.mdx‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ SentrySdk.Init(options =>
3232
});
3333
```
3434

35-
To drop every log and keep sending errors and breadcrumbs, return `null`:
35+
To drop every log and keep sending errors and breadcrumbs, return `null` from `SetBeforeSendLog`. The callback works for every integration and for `SentrySdk.Logger`. Some integrations also have their own switch, which you can set in configuration and which stops the SDK from creating those logs at all:
36+
37+
- For `ILogger`, turn off the `Sentry` logging provider, as shown below.
38+
- For NLog, set [`minimumLogLevel`](/platforms/dotnet/guides/nlog/logs/#minimumloglevel) to `Off` on the target.
39+
40+
The Serilog sink and the log4net appender have no such switch, so use the callback for those.
41+
42+
With the callback, return `null`:
3643

3744
```csharp {filename:Program.cs}
3845
SentrySdk.Init(options =>
@@ -42,7 +49,7 @@ SentrySdk.Init(options =>
4249
});
4350
```
4451

45-
In apps that call `UseSentry`, set the same callback on its options. The callback applies to every log, including logs from `SentrySdk.Logger` and from the `ILogger` integration.
52+
In apps that call `UseSentry`, set the same callback on its options:
4653

4754
```csharp {filename:Program.cs}
4855
var builder = WebApplication.CreateBuilder(args);
@@ -53,4 +60,29 @@ builder.UseSentry(options =>
5360
});
5461
```
5562

56-
See [Logs](/platforms/dotnet/logs/) for more about filtering logs.
63+
To turn off the `Sentry` logging provider, set its level to `None`. If your app reads the `Logging` section from configuration, as `WebApplication.CreateBuilder` and `Host.CreateApplicationBuilder` do, set it in `appsettings.json`:
64+
65+
```json {filename:appsettings.json}
66+
{
67+
"Logging": {
68+
"Sentry": {
69+
"LogLevel": {
70+
"Default": "None"
71+
}
72+
}
73+
}
74+
}
75+
```
76+
77+
Otherwise, add the same rule when you add the integration:
78+
79+
```csharp {filename:Program.cs}
80+
using var loggerFactory = LoggerFactory.Create(builder =>
81+
{
82+
builder.AddSentry();
83+
builder.Services.Configure<LoggerFilterOptions>(options =>
84+
options.Rules.Add(new LoggerFilterRule("Sentry", null, LogLevel.None, null)));
85+
});
86+
```
87+
88+
See [Logs](/platforms/dotnet/logs/) for more about filtering logs, and [Configure Microsoft.Extensions.Logging Filters](/platforms/dotnet/guides/extensions-logging/logs/#configure-microsoftextensionslogging-filters) for more about the `Sentry` provider.

‎platform-includes/logs/options/dotnet.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ In earlier versions, `EnableLogs` was an option on `SentryOptions`, and the Seri
1818

1919
#### Configure Microsoft.Extensions.Logging Filters
2020

21-
Sentry's `Microsoft.Extensions.Logging` provider defines the alias `Sentry`.
21+
Sentry's `Microsoft.Extensions.Logging` provider defines the alias `Sentry`, so you can use `Sentry` as the provider name in your filters. A `LogLevel` section under `Logging:Sentry` applies only to the logs Sentry sends. Its keys are categories, the same as in the top-level `LogLevel` section.
2222

2323
Logs emitted through `ILogger<T>` are sent to Sentry according to your configured provider/category filters and log levels.
2424

0 commit comments

Comments
 (0)