|
| 1 | +Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the `SentrySdk.Experimental.Logger` APIs. |
| 2 | + |
| 3 | +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`. |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +```csharp |
| 8 | +SentrySdk.Experimental.Logger.LogInfo("A simple log message"); |
| 9 | +SentrySdk.Experimental.Logger.LogError("A {0} log message", ["formatted"]); |
| 10 | +``` |
| 11 | + |
| 12 | +<Alert title="Note"> |
| 13 | +During the experimental phase of the feature, we will provide more method overloads for convenient invocation in common scenarios. |
| 14 | +Additionally, we may provide method overloads that are not based on _composite format strings_, but on _interpolated strings_. |
| 15 | +</Alert> |
| 16 | + |
| 17 | +<PlatformSection supported={["dotnet.aspnetcore, dotnet.azure-functions-worker, dotnet.blazor-webassembly, dotnet.extensions-logging, dotnet.maui"]}> |
| 18 | + |
| 19 | +Additionally, when enabled and initialized, the SDK registers a [ILoggerProvider](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.iloggerprovider) |
| 20 | +that allows sending logs to Sentry via [ILogger<T>](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.ilogger-1) instances resolved through _.NET dependency injection_. |
| 21 | + |
| 22 | +```csharp |
| 23 | +public sealed class MyService(ILogger<MyService> logger) |
| 24 | +{ |
| 25 | + public async Task InvokeAsync(HttpContext context) |
| 26 | + { |
| 27 | + logger.LogInformation("A simple log message"); |
| 28 | + await Task.Yield(); |
| 29 | + logger.LogError("A {Parameter} log message", "formatted"); |
| 30 | + await Task.Delay(TimeSpan.FromSeconds(1)); |
| 31 | + logger.LogWarning(new EventId(1, nameof(InvokeAsync)), "Message with EventId"); |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +The `ILogger<T>`'s _CategoryName_, as well as the `EventId` (if provided), are attached as attributes to the logs. |
| 37 | + |
| 38 | +For more information, see article on [Logging in C# and .NET](https://learn.microsoft.com//dotnet/core/extensions/logging). |
| 39 | +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. |
| 40 | + |
| 41 | +</PlatformSection> |
| 42 | + |
| 43 | +The SDK automatically provides a set of default attributes attached to your logs. |
| 44 | +Additionally, you can attach custom attributes via a delegate. |
| 45 | + |
| 46 | +```csharp |
| 47 | +SentrySdk.Experimental.Logger.LogWarning("A log message with additional attributes.", [], static log => |
| 48 | +{ |
| 49 | + log.SetAttribute("my.attribute", "value"); |
| 50 | +}); |
| 51 | +``` |
| 52 | + |
| 53 | +<Alert title="Note"> |
| 54 | +Please note that we will revise the API shape to set custom attributes during the experimental phase of the feature. |
| 55 | +</Alert> |
| 56 | + |
| 57 | +Supported attribute types are: |
| 58 | +- Textual: `string` and `char` |
| 59 | +- Logical: `bool` |
| 60 | +- Integral: `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long` and `nint` |
| 61 | +- Floating-point: `float` and `double` |
| 62 | + |
| 63 | +Unsupported numeric types such as `ulong`, `nuint`, `decimal`, as well as all other types including `object`, are treated as `string` via `ToString`. |
0 commit comments