Serilog.Sinks.OpenTelemetry 2.0.0-dev-00259

Prefix Reserved
This is a prerelease version of Serilog.Sinks.OpenTelemetry.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Serilog.Sinks.OpenTelemetry --version 2.0.0-dev-00259
                    
NuGet\Install-Package Serilog.Sinks.OpenTelemetry -Version 2.0.0-dev-00259
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Serilog.Sinks.OpenTelemetry" Version="2.0.0-dev-00259" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Serilog.Sinks.OpenTelemetry" Version="2.0.0-dev-00259" />
                    
Directory.Packages.props
<PackageReference Include="Serilog.Sinks.OpenTelemetry" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Serilog.Sinks.OpenTelemetry --version 2.0.0-dev-00259
                    
#r "nuget: Serilog.Sinks.OpenTelemetry, 2.0.0-dev-00259"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Serilog.Sinks.OpenTelemetry&version=2.0.0-dev-00259&prerelease
                    
Install Serilog.Sinks.OpenTelemetry as a Cake Addin
#tool nuget:?package=Serilog.Sinks.OpenTelemetry&version=2.0.0-dev-00259&prerelease
                    
Install Serilog.Sinks.OpenTelemetry as a Cake Tool

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.

⚠️ 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.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (44)

Showing the top 5 NuGet packages that depend on Serilog.Sinks.OpenTelemetry:

Package Downloads
Sitko.Core.App

Sitko.Core is a set of libraries to help build .NET Core applications fast

MyJetWallet.Sdk.Service

Package Description

Genocs.Logging

The logging library.

CucurbIT.Infrastructure.Logging.Targets

Package Description

Relativity.Transfer.SDK

Relativity Transfer SDK allows performing high-throughput transfers of files from and to Relativity environment.

GitHub repositories (12)

Showing the top 12 popular GitHub repositories that depend on Serilog.Sinks.OpenTelemetry:

Repository Stars
fullstackhero/dotnet-starter-kit
Production Grade Cloud-Ready .NET 9 Starter Kit (Web API + Blazor Client) with Multitenancy Support, and Clean/Modular Architecture that saves roughly 200+ Development Hours! All Batteries Included.
SciSharp/BotSharp
AI Multi-Agent Framework in .NET
featbit/featbit
Enterprise-grade feature flag platform that you can self-host. Get started - free.
abpframework/abp-samples
Sample solutions built with the ABP Framework
mehdihadeli/food-delivery-microservices
🍔 A practical and imaginary food delivery microservices, built with .Net 9, MassTransit, Domain-Driven Design, CQRS, Vertical Slice Architecture, Event-Driven Architecture, and the latest technologies.
AIDotNet/Thor
Thor(雷神托尔) 是一款强大的人工智能模型管理工具,其主要目的是为了实现多种AI模型的统一管理和使用。通过Thor(雷神托尔),用户可以轻松地管理和使用众多AI模型,而且Thor(雷神托尔)兼容OpenAI的接口格式,使得使用更加方便。
sitkoru/Sitko.Core
Sitko.Core is a set of libraries to help build .NET Core applications fast
serilog-tracing/serilog-tracing
A minimal tracing system that integrates Serilog with System.Diagnostics.Activity.
ZaqueuCavalcante/syki
Full Education Management System.
SapiensAnatis/Dawnshard
Server emulator for Dragalia Lost
baranacikgoz/modular-monolith-ddd-vsa-webapi
A .NET 9 Webapi boilerplate with Modular Monolith approach, Domain-Driven Design and Vertical Slices architecture along with Clean Architecture principles per feature.
marinasundstrom/YourBrand
Prototype enterprise system for e-commerce and consulting services
Version Downloads Last updated
4.2.0-dev-02302 1,844 3/18/2025
4.1.1 2,515,763 9/24/2024
4.1.1-dev-00356 94 9/24/2024
4.1.1-dev-00351 858 9/18/2024
4.1.0 412,606 9/5/2024
4.1.0-dev-00344 112 9/5/2024
4.1.0-dev-00336 5,913 8/20/2024
4.1.0-dev-00333 2,923 8/19/2024
4.0.0 667,671 7/28/2024
4.0.0-dev-00325 1,485 7/26/2024
4.0.0-dev-00322 503 7/23/2024
4.0.0-dev-00317 5,735 7/16/2024
4.0.0-dev-00315 1,148 7/12/2024
4.0.0-dev-00313 4,299 6/25/2024
3.0.1-dev-00309 232 6/25/2024
3.0.0 761,451 6/6/2024
3.0.0-dev-00300 127 6/6/2024
3.0.0-dev-00298 26,433 5/8/2024
2.0.0 397,851 5/7/2024
2.0.0-dev-00289 142 5/7/2024
2.0.0-dev-00284 151 5/7/2024
2.0.0-dev-00282 267,070 3/18/2024
2.0.0-dev-00270 29,132 1/4/2024
2.0.0-dev-00261 853 1/2/2024
2.0.0-dev-00259 2,968 12/7/2023
1.2.0 1,847,778 11/15/2023
1.2.0-dev-00255 407 11/15/2023
1.2.0-dev-00253 513 11/9/2023
1.2.0-dev-00247 6,508 10/11/2023
1.2.0-dev-00243 528 10/3/2023
1.1.0 478,490 9/28/2023
1.1.0-dev-00239 495 9/28/2023
1.1.0-dev-00236 886 9/18/2023
1.0.3-dev-00230 5,877 8/24/2023
1.0.2 446,657 7/11/2023
1.0.2-dev-00227 653 7/10/2023
1.0.1 53,788 7/7/2023
1.0.1-dev-00223 640 7/7/2023
1.0.1-dev-00222 632 7/7/2023
1.0.1-dev-00218 6,450 6/13/2023
1.0.0 325,429 6/1/2023
1.0.0-dev-00214 1,637 5/30/2023
1.0.0-dev-00212 1,799 5/29/2023
1.0.0-dev-00208 20,885 5/26/2023
1.0.0-dev-00204 7,376 5/18/2023
1.0.0-dev-00202 1,692 5/17/2023
1.0.0-dev-00200 731 5/17/2023
1.0.0-dev-00194 929 5/15/2023
1.0.0-dev-00192 702 5/15/2023
1.0.0-dev-00188 2,989 5/5/2023
1.0.0-dev-00182 179 5/5/2023
1.0.0-dev-00178 610 5/4/2023
1.0.0-dev-00175 1,262 5/3/2023
1.0.0-dev-00173 1,849 5/2/2023
1.0.0-dev-00166 2,227 5/1/2023
1.0.0-dev-00161 197 4/30/2023
1.0.0-dev-00152 234 4/29/2023
1.0.0-dev-00151 2,359 4/29/2023
1.0.0-dev-00148 189 4/29/2023
1.0.0-dev-00143 5,454 4/24/2023
1.0.0-dev-00142 187 4/24/2023
1.0.0-dev-00141 188 4/24/2023
1.0.0-dev-00129 695 4/19/2023
1.0.0-dev-00128 214 4/19/2023
1.0.0-dev-00121 708 4/14/2023
1.0.0-dev-00120 335 4/14/2023
1.0.0-dev-00117 479 4/12/2023
1.0.0-dev-00113 56,913 3/14/2023
1.0.0-dev-00098 52,665 2/12/2023
1.0.0-dev-00091 147 2/12/2023
1.0.0-dev-00080 147 2/10/2023
0.5.0-dev-00078 1,396 2/9/2023
0.4.0-dev-00073 24,753 2/3/2023
0.2.0-dev-00063 7,334 1/23/2023
0.2.0-dev-00059 239 1/10/2023
0.1.0-dev-00048 132 1/10/2023
0.0.4-dev-00039 64,022 1/9/2023
0.0.3-dev-00036 138 1/9/2023
0.0.2-dev-00027 147 1/7/2023
0.0.1-dev-00018 134 1/4/2023
0.0.1-dev-00015 133 1/3/2023
0.0.1-dev-00013 137 1/3/2023