diff --git a/platform-includes/logs/options/dotnet.mdx b/platform-includes/logs/options/dotnet.mdx index 7fab5cabe44f6..7d20f79800a62 100644 --- a/platform-includes/logs/options/dotnet.mdx +++ b/platform-includes/logs/options/dotnet.mdx @@ -12,9 +12,59 @@ Set to `true` in order to enable the logging integration via the `ILogger + + +#### Configure Microsoft.Extensions.Logging Filters + +In apps that use `Microsoft.Extensions.Logging`, setting `EnableLogs = true` enables Sentry's `Microsoft.Extensions.Logging` integration. +Sentry's provider defines the alias `Sentry`. + +Logs emitted through `ILogger` are sent to Sentry according to your configured provider/category filters and log levels. + +Use standard `Microsoft.Extensions.Logging` filtering to reduce noisy categories or disable the Sentry provider: + +```json {filename:appsettings.json} +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + }, + "Sentry": { + "LogLevel": { + "Default": "None" + } + } + } +} +``` + +Setting `Logging:Sentry:LogLevel:Default` to `None` disables logs sent through the `Microsoft.Extensions.Logging` provider for all categories, while `EnableLogs = true` keeps the `SentrySdk.Logger` APIs available. + +Example with `EnableLogs = true` and `Logging:Sentry:LogLevel:Default = None`: + +```csharp +logger.LogInformation("Will not be sent to Sentry through Microsoft.Extensions.Logging"); +SentrySdk.Logger.LogInfo("Still sent via SentrySdk.Logger"); +``` + +You can also configure filters in code: + +```csharp +// Default minimum level for all categories +builder.Logging.AddFilter(null, LogLevel.Warning); +// Override for a specific category +builder.Logging.AddFilter("MyApp.Namespace", LogLevel.Information); +``` + + +For background on provider/category filtering, see [Logging in C# and .NET](https://learn.microsoft.com/dotnet/core/extensions/logging/overview#configure-logging). + + + #### SetBeforeSendLog -To filter logs, or update them before they are sent to Sentry, you can use the `SetBeforeSendLog(Func)` option. +To filter logs or update them before they are sent to Sentry, you can use the `SetBeforeSendLog(Func)` option. ```csharp options => diff --git a/platform-includes/logs/setup/dotnet.mdx b/platform-includes/logs/setup/dotnet.mdx index dc614c6db66d9..d1b4937716233 100644 --- a/platform-includes/logs/setup/dotnet.mdx +++ b/platform-includes/logs/setup/dotnet.mdx @@ -11,6 +11,9 @@ SentrySdk.Init(options => }); ``` + +This enables the `SentrySdk.Logger` APIs. + @@ -24,6 +27,9 @@ SentrySdk.Init(options => }); ``` + +This enables the `SentrySdk.Logger` APIs, as well as the `Microsoft.Extensions.Logging` integration. + @@ -63,4 +69,8 @@ SentrySdk.Init(options => }); ``` + + +It does _not_ capture the `Console.WriteLine()` standard output stream. + diff --git a/platform-includes/logs/usage/dotnet.mdx b/platform-includes/logs/usage/dotnet.mdx index 954010a4df43a..19ae34be91bcc 100644 --- a/platform-includes/logs/usage/dotnet.mdx +++ b/platform-includes/logs/usage/dotnet.mdx @@ -15,8 +15,9 @@ SentrySdk.Logger.LogError("A {0} log message", "formatted"); -Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using instances of the [ILogger](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.ilogger-1) interface, -resolved through _.NET dependency injection_. +Once the feature is enabled and the SDK is initialized, you can send logs using instances of the [ILogger](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.ilogger-1) interface, resolved through [.NET dependency injection](https://learn.microsoft.com/dotnet/core/extensions/dependency-injection/overview). + +Sentry respects your existing `Microsoft.Extensions.Logging` provider/category configuration. The `LoggerExtensions` extension methods expose various overloads that you can use to log messages at six different log levels automatically mapped to Sentry's severity: