Documentation
DocumentationDiscussions
Documentation

OpenTelemetry .NET SDK

Configuring Microsoft.Extensions.Logging with OpenTelemetry and Seq

ASP.NET Core and other .NET applications that use Microsoft.Extensions.Logging without Serilog can use the OpenTelemetry.Exporter.OpenTelemetryProtocol.Logs NuGet package to send data. To collect traces as well as logs see Tracing from .NET.

📘

This page describes Microsoft.Extensions.Logging

Applications that use Serilog as the underlying logging pipeline (UseSerilog()) need to configure Serilog.Sinks.OpenTelemetry instead.

First, add the following NuGet package references to your application:

dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol.Logs --prerelease  
dotnet add package OpenTelemetry.Instrumentation.AspNetCore --prerelease

Then, in Program.cs you'll need to hook into the AddLogging() method and add an OTLP exporter:

var builder = WebApplication.CreateBuilder(args);

// Other configuration ...

builder.Services.AddLogging(logging => logging.AddOpenTelemetry(openTelemetryLoggerOptions =>  
{  
    openTelemetryLoggerOptions.SetResourceBuilder(  
        ResourceBuilder.CreateEmpty()
            // Replace "GettingStarted" with the name of your application
            .AddService("GettingStarted")  
            .AddAttributes(new Dictionary<string, object>  
            {
                // Add any desired resource attributes here
                ["deployment.environment"] = "development"  
            }));
  
    // Some important options to improve data quality
    openTelemetryLoggerOptions.IncludeScopes = true;
    openTelemetryLoggerOptions.IncludeFormattedMessage = true;

    openTelemetryLoggerOptions.AddOtlpExporter(exporter =>
    {
        // The full endpoint path is required here, when using
        // the `HttpProtobuf` protocol option.
        exporter.Endpoint = new Uri("http://localhost:5341/ingest/otlp/v1/logs");
        exporter.Protocol = OtlpExportProtocol.HttpProtobuf;
        // Optional `X-Seq-ApiKey` header for authentication, if required
        exporter.Headers = "X-Seq-ApiKey=abcde12345"
    });
}));

var app = builder.Build();

The IncludeScopes option ensures properties attached through ILogger.BeginScope()`` get transmitted to Seq. The IncludeFormattedMessage option ensures Seq can reliably identify the original message template that produced the event - it's possible there will be some change in this area down the track.

If everything is configured correctly, launching your application will cause some logs to show up in the Seq Events screen:

Seq Events screen showing OTLP events ingested from ASP.NET Core.

Seq Events screen showing OTLP events ingested from ASP.NET Core.

📘

gRPC protocol flavor

Seq also supports the OTLP/gRPC protocol flavor; see Ingestion with OpenTelemetry for further information.