Serilog.Sinks.OpenTelemetry 4.0.0-dev-00315

Serilog.Sinks.OpenTelemetry Build status NuGet Version

This Serilog sink transforms Serilog events into OpenTelemetry LogRecords and sends them to an OTLP (gRPC or HTTP) endpoint.

The sink aims for full compliance with the OpenTelemetry Logs protocol. It does not depend on the OpenTelemetry SDK or .NET API.

OpenTelemetry supports attributes with scalar values, arrays, and maps. Serilog does as well. Consequently, the sink does a one-to-one mapping between Serilog properties and OpenTelemetry attributes. There is no flattening, renaming, or other modifications done to the properties by default.

Getting started

To use the OpenTelemetry sink, first install the NuGet package:

dotnet add package Serilog.Sinks.OpenTelemetry

Then enable the sink using WriteTo.OpenTelemetry():

Log.Logger = new LoggerConfiguration()
    .WriteTo.OpenTelemetry()
    .CreateLogger();

Generate logs using the Log.Information(...) and similar methods to send transformed logs to a local OpenTelemetry OTLP endpoint.

A more complete configuration would specify Endpoint, Protocol, and other parameters, such asResourceAttributes, as shown in the examples below.

Configuration

This sink supports two configuration styles: inline and options. The inline configuration looks like:

Log.Logger = new LoggerConfiguration()
    .WriteTo.OpenTelemetry(
        endpoint: "http://127.0.0.1:4318/v1/logs",
        protocol: OtlpProtocol.HttpProtobuf)
    .CreateLogger();

This configuration is appropriate only for simple, local logging setups.

More complicated use cases will need to use the options configuration, which looks like:

Log.Logger = new LoggerConfiguration()
    .WriteTo.OpenTelemetry(options =>
    {
        options.Endpoint = "http://127.0.0.1:4318/v1/logs";
        options.Protocol = OtlpProtocol.HttpProtobuf;
    })
    .CreateLogger();

This supports the sink's full set of configuration options. See the OpenTelemetrySinkOptions.cs file for the full set of options. Some of the more imporant parameters are discussed in the following sections.

Endpoint and protocol

The default endpoint is http://localhost:4317, which will send logs to an OpenTelemetry collector running on the same machine over the gRPC protocol. This is appropriate for testing or for using a local OpenTelemetry collector as a proxy for a downstream logging service.

In most production scenarios, you will want to set an endpoint. To do so, add the endpoint argument to the WriteTo.OpenTelemetry() call.

You may also want to set the protocol explicitly. The supported values are:

  • OtlpProtocol.Grpc: Sends a protobuf representation of the OpenTelemetry Logs over a gRPC connection (the default).
  • OtlpProtocol.HttpProtobuf: Sends a protobuf representation of the OpenTelemetry Logs over an HTTP connection.

When the OtlpProtocol.HttpProtobuf option is specified, the endpoint URL must include the full path, for example http://localhost:4318/v1/logs.

Resource attributes

OpenTelemetry logs may contain a "resource" that provides metadata concerning the entity associated with the logs, typically a service or library. These may contain "resource attributes" and are emitted for all logs flowing through the configured logger.

These resource attributes may be provided as a Dictionary<string, Object> when configuring a logger. OpenTelemetry allows resource attributes with rich values; however, this implementation only supports resource attributes with primitive values.

:warning: Resource attributes with non-primitive values will be silently ignored.

This example shows how the resource attributes can be specified when the logger is configured.

Log.Logger = new LoggerConfiguration()
    .WriteTo.OpenTelemetry(options =>
    {
        options.Endpoint = "http://127.0.0.1:4317";
        options.ResourceAttributes = new Dictionary<string, object>
        {
            ["service.name"] = "test-logging-service",
            ["index"] = 10,
            ["flag"] = true,
            ["value"] = 3.14
        };
    })
    .CreateLogger();

Serilog LogEvent to OpenTelemetry log record mapping

The following table provides the mapping between the Serilog log events and the OpenTelemetry log records.

Serilog LogEvent OpenTelemetry LogRecord Comments
Exception.GetType().ToString() Attributes["exception.type"]
Exception.Message Attributes["exception.message"] Ignored if empty
Exception.StackTrace Attributes[ "exception.stacktrace"] Value of ex.ToString()
Level SeverityNumber Serilog levels are mapped to corresponding OpenTelemetry severities
Level.ToString() SeverityText
Message Body Culture-specific formatting can be provided via sink configuration
MessageTemplate Attributes[ "message_template.text"] Requires IncludedData. MessageTemplateText (enabled by default)
MessageTemplate (MD5) Attributes[ "message_template.hash.md5"] Requires IncludedData. MessageTemplateMD5 HashAttribute
Properties Attributes Each property is mapped to an attribute keeping the name; the value's structure is maintained
SpanId (Activity.Current) SpanId Requires IncludedData.SpanId (enabled by default)
Timestamp TimeUnixNano .NET provides 100-nanosecond precision
TraceId (Activity.Current) TraceId Requires IncludedData.TraceId (enabled by default)

Configuring included data

This sink supports configuration of how common OpenTelemetry fields are populated from the Serilog LogEvent and .NET Activity context via the IncludedData flags enum:

Log.Logger = new LoggerConfiguration()
    .WriteTo.OpenTelemetry(options =>
    {
        options.Endpoint = "http://127.0.0.1:4317";
        options.IncludedData: IncludedData.MessageTemplate |
                              IncludedData.TraceId | IncludedData.SpanId;
    })
    .CreateLogger();

The example shows the default value; IncludedData.MessageTemplateMD5HashAttribute can also be used to add the MD5 hash of the message template.

Example

The example/Example subdirectory contains an example application that logs to a local OpenTelemetry collector. See the README in that directory for instructions on how to run the example.

Copyright © Serilog Contributors - Provided under the Apache License, Version 2.0.

No packages depend on Serilog.Sinks.OpenTelemetry.

.NET Framework 4.6.2

.NET Framework 4.7.1

.NET 6.0

.NET 8.0

.NET Standard 2.0

Version Downloads Last updated
4.2.0 12 06/05/2025
4.2.0-dev-02303 10 06/05/2025
4.2.0-dev-02302 10 03/28/2025
4.1.1 13 03/28/2025
4.1.1-dev-00356 12 03/28/2025
4.1.1-dev-00351 12 03/28/2025
4.1.0 13 03/28/2025
4.1.0-dev-00344 12 03/28/2025
4.1.0-dev-00336 12 03/28/2025
4.1.0-dev-00333 12 03/28/2025
4.0.0 12 03/28/2025
4.0.0-dev-00325 11 03/28/2025
4.0.0-dev-00322 11 03/28/2025
4.0.0-dev-00317 11 03/28/2025
4.0.0-dev-00315 12 03/28/2025
4.0.0-dev-00313 11 03/28/2025
3.0.1-dev-00309 11 03/28/2025
3.0.0 12 03/28/2025
3.0.0-dev-00300 11 03/28/2025
3.0.0-dev-00298 11 03/28/2025
2.0.0 32 09/19/2024
2.0.0-dev-00289 11 03/28/2025
2.0.0-dev-00284 11 03/28/2025
2.0.0-dev-00282 11 03/28/2025
2.0.0-dev-00270 11 03/28/2025
2.0.0-dev-00261 11 03/28/2025
2.0.0-dev-00259 11 03/28/2025
1.2.0 12 03/28/2025
1.2.0-dev-00255 11 03/28/2025
1.2.0-dev-00253 12 03/28/2025
1.2.0-dev-00247 12 03/28/2025
1.2.0-dev-00243 12 03/28/2025
1.1.0 12 03/28/2025
1.1.0-dev-00239 12 03/28/2025
1.1.0-dev-00236 12 03/28/2025
1.0.3-dev-00230 12 03/28/2025
1.0.2 12 03/28/2025
1.0.2-dev-00227 11 03/28/2025
1.0.1 12 03/28/2025
1.0.1-dev-00223 11 03/28/2025
1.0.1-dev-00222 11 03/28/2025
1.0.1-dev-00218 11 03/28/2025
1.0.0 12 03/28/2025
1.0.0-dev-00214 11 03/28/2025
1.0.0-dev-00212 11 03/28/2025
1.0.0-dev-00208 11 03/28/2025
1.0.0-dev-00204 11 03/28/2025
1.0.0-dev-00202 11 03/28/2025
1.0.0-dev-00200 11 03/28/2025
1.0.0-dev-00194 12 03/28/2025
1.0.0-dev-00192 11 03/28/2025
1.0.0-dev-00188 12 03/28/2025
1.0.0-dev-00182 11 03/28/2025
1.0.0-dev-00178 11 03/28/2025
1.0.0-dev-00175 12 03/28/2025
1.0.0-dev-00173 12 03/28/2025
1.0.0-dev-00166 11 03/28/2025
1.0.0-dev-00161 11 03/28/2025
1.0.0-dev-00152 11 03/28/2025
1.0.0-dev-00151 11 03/28/2025
1.0.0-dev-00148 11 03/28/2025
1.0.0-dev-00143 11 03/28/2025
1.0.0-dev-00142 11 03/28/2025
1.0.0-dev-00141 11 03/28/2025
1.0.0-dev-00129 11 03/28/2025
1.0.0-dev-00128 11 03/28/2025
1.0.0-dev-00121 10 03/28/2025
1.0.0-dev-00120 11 03/28/2025
1.0.0-dev-00117 11 03/28/2025
1.0.0-dev-00113 11 03/28/2025
1.0.0-dev-00098 11 03/28/2025
1.0.0-dev-00091 11 03/28/2025
1.0.0-dev-00080 11 03/28/2025
0.5.0-dev-00078 11 03/28/2025
0.4.0-dev-00073 11 03/28/2025
0.2.0-dev-00063 11 03/28/2025
0.2.0-dev-00059 11 03/28/2025
0.1.0-dev-00048 11 03/28/2025
0.0.4-dev-00039 11 03/28/2025
0.0.3-dev-00036 11 03/28/2025
0.0.2-dev-00027 11 03/28/2025
0.0.1-dev-00018 11 03/28/2025
0.0.1-dev-00015 11 03/28/2025
0.0.1-dev-00013 11 03/28/2025