|
| 1 | +<PlatformSection notSupported={["dotnet.aspnetcore", "dotnet.azure-functions-worker", "dotnet.blazor-webassembly", "dotnet.extensions-logging", "dotnet.maui"]}> |
| 2 | + |
| 3 | +Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the `SentrySdk.Experimental.Logger` APIs. |
| 4 | + |
| 5 | +The `SentrySdk.Experimental.Logger` instance exposes six methods that you can use to log messages at different log levels: `Trace`, `Debug`, `Info`, `Warning`, `Error`, and `Fatal`. |
| 6 | + |
| 7 | +These properties will be sent to Sentry, and can be searched from within the Logs UI, and even added to the Logs views as a dedicated column. |
| 8 | + |
| 9 | +```csharp |
| 10 | +SentrySdk.Experimental.Logger.LogInfo("A simple log message"); |
| 11 | +SentrySdk.Experimental.Logger.LogError("A {0} log message", ["formatted"]); |
| 12 | +``` |
| 13 | + |
| 14 | +<Alert title="Note"> |
| 15 | +During the experimental phase of the feature, we will provide more method overloads for convenient invocation in common scenarios. |
| 16 | +Additionally, we may provide method overloads that are not based on _composite format strings_, but on _interpolated strings_. |
| 17 | +</Alert> |
| 18 | + |
| 19 | +</PlatformSection> |
| 20 | + |
| 21 | +<PlatformSection supported={["dotnet.aspnetcore", "dotnet.azure-functions-worker", "dotnet.blazor-webassembly", "dotnet.extensions-logging", "dotnet.maui"]}> |
| 22 | + |
| 23 | +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, |
| 24 | +resolved through _.NET dependency injection_. |
| 25 | + |
| 26 | +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: |
| 27 | + |
| 28 | +| Microsoft.Extensions.Logging.LogLevel | Sentry.SentryLogLevel | Sentry Logs UI Severity | |
| 29 | +| --- | --- | --- | |
| 30 | +| Trace | Trace | TRACE | |
| 31 | +| Debug | Debug | DEBUG | |
| 32 | +| Information | Info | INFO | |
| 33 | +| Warning | Warning | WARN | |
| 34 | +| Error | Error | ERROR | |
| 35 | +| Critical | Fatal | FATAL | |
| 36 | + |
| 37 | +These properties will be sent to Sentry, and can be searched from within the Logs UI, and even added to the Logs views as a dedicated column. |
| 38 | + |
| 39 | +```csharp |
| 40 | +public sealed class MyService(ILogger<MyService> logger) |
| 41 | +{ |
| 42 | + public void Invoke() |
| 43 | + { |
| 44 | + logger.LogInformation("A simple log message"); |
| 45 | + logger.LogError("A {Parameter} log message", "formatted"); |
| 46 | + logger.LogWarning(new EventId(1, nameof(Invoke)), "Message with EventId"); |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +The `ILogger`'s _CategoryName_, as well as the `EventId` (if provided), are attached as attributes to the logs. |
| 52 | + |
| 53 | +For more information, see the article on [Logging in C# and .NET](https://learn.microsoft.com//dotnet/core/extensions/logging). |
| 54 | +Sentry Structured Logs also work with [High-performance logging in .NET](https://learn.microsoft.com/dotnet/core/extensions/high-performance-logging) and [Compile-time logging source generation](https://learn.microsoft.com/dotnet/core/extensions/logger-message-generator) alike. |
| 55 | + |
| 56 | +</PlatformSection> |
| 57 | + |
| 58 | +<PlatformSection notSupported={["dotnet.aspnetcore", "dotnet.azure-functions-worker", "dotnet.blazor-webassembly", "dotnet.extensions-logging", "dotnet.maui"]}> |
| 59 | + |
| 60 | +The SDK automatically provides a set of default attributes attached to your logs. |
| 61 | +Additionally, you can attach custom attributes via a delegate. |
| 62 | + |
| 63 | +```csharp |
| 64 | +SentrySdk.Experimental.Logger.LogWarning("A log message with additional attributes.", [], static log => |
| 65 | +{ |
| 66 | + log.SetAttribute("my.attribute", "value"); |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +<Alert title="Note"> |
| 71 | +Please note that we will revise the API shape to set custom attributes during the experimental phase of the feature. |
| 72 | +</Alert> |
| 73 | + |
| 74 | +Supported attribute types are: |
| 75 | +- Textual: `string` and `char` |
| 76 | +- Logical: `bool` |
| 77 | +- Integral: `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long` and `nint` |
| 78 | +- Floating-point: `float` and `double` |
| 79 | + |
| 80 | +Unsupported numeric types such as `ulong`, `nuint`, `decimal`, as well as all other types including `object`, are treated as `string` via `ToString()`. |
| 81 | + |
| 82 | +</PlatformSection> |
0 commit comments