Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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.aws-lambda", "dotnet.azure-functions-worker", "dotnet.blazor-webassembly", "dotnet.extensions-logging", "dotnet.maui"]}>

#### 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`.

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 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).

</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
10 changes: 10 additions & 0 deletions platform-includes/logs/setup/dotnet.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ SentrySdk.Init(options =>
});
```


This enables the `SentrySdk.Logger` APIs.

</PlatformSection>

<PlatformSection supported={["dotnet.aspnetcore", "dotnet.aws-lambda", "dotnet.azure-functions-worker", "dotnet.blazor-webassembly", "dotnet.maui"]}>
Expand All @@ -24,6 +27,9 @@ SentrySdk.Init(options =>
});
```


This enables the `SentrySdk.Logger` APIs, as well as the `Microsoft.Extensions.Logging` integration.

</PlatformSection>

<PlatformSection supported={["dotnet.extensions-logging"]}>
Expand Down Expand Up @@ -63,4 +69,8 @@ SentrySdk.Init(options =>
});
```


</PlatformSection>

It does _not_ capture the `Console.WriteLine()` standard output stream.

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](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:

Expand Down
Loading