Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
52 changes: 51 additions & 1 deletion platform-includes/logs/options/dotnet.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,59 @@ Set to `true` in order to enable the logging integration via the `ILogger<TCateg
Set to `true` in order to enable the logging integration via the `Log`/`Logger` APIs.
</PlatformSection>

<PlatformSection supported={["dotnet.aspnetcore", "dotnet.azure-functions-worker", "dotnet.blazor-webassembly", "dotnet.extensions-logging", "dotnet.maui"]}>
Comment thread
KyleTryon marked this conversation as resolved.
Outdated

#### Configure Microsoft.Extensions.Logging Filters

In apps that use `Microsoft.Extensions.Logging`, setting `EnableLogs = true` enables Sentry's `Microsoft.Extensions.Logging` integration.

Comment thread
KyleTryon marked this conversation as resolved.
Logs emitted through `ILogger<T>` 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, while `EnableLogs = true` keeps the `SentrySdk.Logger` APIs available.
Comment thread
KyleTryon marked this conversation as resolved.
Outdated

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 framework-specific setup details, see [Microsoft.Extensions.Logging for .NET](https://docs.sentry.io/platforms/dotnet/guides/extensions-logging/logs/).
Comment thread
KyleTryon marked this conversation as resolved.
Outdated
Comment thread
KyleTryon marked this conversation as resolved.
Outdated

For background on provider/category filtering, see [Logging in C# and .NET](https://learn.microsoft.com/dotnet/core/extensions/logging/overview#configure-logging).

</PlatformSection>

#### SetBeforeSendLog

To filter logs, or update them before they are sent to Sentry, you can use the `SetBeforeSendLog(Func<SentryLog, SentryLog?>)` option.
To filter logs or update them before they are sent to Sentry, you can use the `SetBeforeSendLog(Func<SentryLog, SentryLog?>)` option.

```csharp
options =>
Expand Down
7 changes: 6 additions & 1 deletion platform-includes/logs/setup/dotnet.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
To enable logging, you need to initialize the SDK with the `EnableLogs` option set to `true`.
Set `EnableLogs` to `true` when initializing the SDK to send logs to Sentry. This enables:

- The `SentrySdk.Logger` APIs
- Supported logging integrations for your platform, such as `Microsoft.Extensions.Logging` and Serilog

It does _not_ capture `Console.WriteLine()` output.
Comment thread
KyleTryon marked this conversation as resolved.
Outdated

<PlatformSection notSupported={["dotnet.aspnetcore", "dotnet.aws-lambda", "dotnet.azure-functions-worker", "dotnet.blazor-webassembly", "dotnet.extensions-logging", "dotnet.maui", "dotnet.serilog"]}>

Expand Down
5 changes: 3 additions & 2 deletions platform-includes/logs/usage/dotnet.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ SentrySdk.Logger.LogError("A {0} log message", "formatted");

<PlatformSection supported={["dotnet.aspnetcore", "dotnet.azure-functions-worker", "dotnet.blazor-webassembly", "dotnet.extensions-logging", "dotnet.maui"]}>

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.
Comment thread
KyleTryon marked this conversation as resolved.
Outdated

Sentry respects your existing `Microsoft.Extensions.Logging` category/provider configuration.
Comment thread
KyleTryon marked this conversation as resolved.
Outdated

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:

Expand Down
Loading