|
1 | | -# serilog-sinks-opentelemetrysdk |
2 | | -An experimental Serilog sink that writes log events directly to the OpenTelemetry SDK. |
| 1 | +# Serilog.Sinks.OpenTelemetrySdk [](https://github.com/serilog/serilog-sinks-opentelemetrysdk/actions) [](https://www.nuget.org/packages/Serilog.Sinks.OpenTelemetrySdk/) |
| 2 | + |
| 3 | +This Serilog sink passes Serilog `LogEvents` to the OpenTelemetry SDK. |
| 4 | + |
| 5 | +> [!IMPORTANT] This package relies on experimental OpenTelemetry features. |
| 6 | +> It will only work with pre-release builds |
| 7 | +> of the .NET OpenTelemetry SDK. Because there's no guarantee that these |
| 8 | +> experimental features will ever ship as stable APIs, there's also no guarantee |
| 9 | +> that a stable version of this sink will ever be available. |
| 10 | +
|
| 11 | +## Getting started |
| 12 | + |
| 13 | +To use this sink, you should already have a **pre-release** (`-beta` or `-rc`) version of the OpenTeleme~~~~try SDK installed |
| 14 | +and configured. |
| 15 | + |
| 16 | +Install the [NuGet package](https://nuget.org/packages/serilog.sinks.opentelemetrysdk): |
| 17 | + |
| 18 | +```shell |
| 19 | +dotnet add package Serilog.Sinks.OpenTelemetrySdk |
| 20 | +``` |
| 21 | + |
| 22 | +Then enable the sink using `WriteTo.OpenTelemetrySdk()`, passing your application's `IServiceProvider`: |
| 23 | + |
| 24 | +```csharp |
| 25 | +// var app = ... |
| 26 | +
|
| 27 | +Log.Logger = new LoggerConfiguration() |
| 28 | + .WriteTo.OpenTelemetrySdk(app.Services) |
| 29 | + .CreateLogger(); |
| 30 | +``` |
| 31 | + |
| 32 | +Logs written with `Log.Information(...)` and similar methods will be output by the configured OpenTelemetry exporters. |
| 33 | + |
| 34 | +## Serilog `LogEvent` to OpenTelemetry log record mapping |
| 35 | + |
| 36 | +The following table provides the mapping between the Serilog log |
| 37 | +events and the OpenTelemetry log records. |
| 38 | + |
| 39 | +Serilog `LogEvent` | OpenTelemetry `LogRecord` | Comments | |
| 40 | +---------------------------------|--------------------------------------------|-----------------------------------------------------------------------------------------------| |
| 41 | +`Exception.GetType().ToString()` | `Attributes["exception.type"]` | | |
| 42 | +`Exception.Message` | `Attributes["exception.message"]` | Ignored if empty | |
| 43 | +`Exception.StackTrace` | `Attributes[ "exception.stacktrace"]` | Value of `ex.ToString()` | |
| 44 | +`Level` | `LogRecordSeverity` | Serilog levels are mapped to corresponding OpenTelemetry severities | |
| 45 | +`Level.ToString()` | `SeverityText` | | |
| 46 | +`Message` | `Body` | Culture-specific formatting can be provided via sink configuration | |
| 47 | +`MessageTemplate` | `Attributes[ "message_template.text"]` | Requires `IncludedData. MessageTemplateText` (enabled by default) | |
| 48 | +`MessageTemplate` (MD5) | `Attributes[ "message_template.hash.md5"]` | Requires `IncludedData. MessageTemplateMD5 HashAttribute` | |
| 49 | +`Properties` | `Attributes` | Each property is mapped to an attribute keeping the name; the value's structure is maintained | |
| 50 | +`SpanId` (`Activity.Current`) | `SpanId` | Requires `IncludedData.SpanIdField` (enabled by default) | |
| 51 | +`Timestamp` | `TimeUnixNano` | .NET provides 100-nanosecond precision | |
| 52 | +`TraceId` (`Activity.Current`) | `TraceId` | Requires `IncludedData.TraceIdField` (enabled by default) | |
| 53 | + |
| 54 | +Serialized data is passed using the `Dictionary<string, object?>` type. Logged arrays are passed as `List<object?>`. |
| 55 | + |
| 56 | +### Configuring included data |
| 57 | + |
| 58 | +This sink supports configuration of how common OpenTelemetry fields are populated from |
| 59 | +the Serilog `LogEvent` by passing `includedData`: |
| 60 | + |
| 61 | +```csharp |
| 62 | +Log.Logger = new LoggerConfiguration() |
| 63 | + .WriteTo.OpenTelemetrySdk(app.Services, includedData: IncludedData.MessageTemplateTextAttribute) |
| 64 | + .CreateLogger(); |
| 65 | +``` |
| 66 | + |
| 67 | +## Acknowledgements |
| 68 | + |
| 69 | +This sink is a fork of [`Serilog.Sinks.OpenTelemetry`](https://github.com/serilog/serilog-sinks-opentelemetry), which |
| 70 | +sends log events using the OpenTelemetry wire protocol, and does not have a dependency on the OpenTelemetry SDK. |
| 71 | + |
| 72 | +_Copyright © Serilog Contributors - Provided under the [Apache License, Version 2.0](http://apache.org/licenses/LICENSE-2.0.html)._ |
0 commit comments