Share via

Updating an ASP.NET Core web application with Serilog to Azure.Monitor.OpenTelemetry.AspNetCore

ritmo2k 871 Reputation points
2026-05-15T13:23:09.0766667+00:00

I am migrating an application from on-premises to Azure and could use some guidance on how to address the changes with correlation id handling. Currently, I use the same pattern utilized in stevejgordon/CorrelationId and update the log format string in Serilog to insert that value in every message. This works well specifically in that log entries for requests are all populated and easy to correlate.

However, I don't think this is required with OpenTelemetry and while a request can be traced in Azure, how do I manage the following two cases:

  • How do I get what will be the correlation id while running in Azure to appear in the console (log stream) when required?
  • What is the generally accepted configuration for local development when I would likely only ever use console/file logging? Thank you!
Developer technologies | ASP.NET Core | ASP.NET API
0 comments No comments

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2026-05-15T13:23:23+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Azure Monitor’s OpenTelemetry distro already creates and flows correlation identifiers (trace/activity IDs) for requests, dependencies, and logs. The goal is to align logging with those built‑in IDs instead of maintaining a separate custom correlation ID system.

    For ASP.NET Core with Azure.Monitor.OpenTelemetry.AspNetCore:

    1. Configure OpenTelemetry and Azure Monitor

    Use the Azure Monitor distro so that traces, metrics, and logs are all correlated and exported to Application Insights:

    <ItemGroup>
      <PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.3.0-beta.1" />
    </ItemGroup>
    

    In Program.cs (before builder.Build()):

    builder.Services.AddOpenTelemetry(otel =>
    {
        // configure tracing, metrics, logging, etc.
    
        if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
        {
            otel.UseAzureMonitor();
        }
    });
    
    builder.Logging.AddOpenTelemetry(); // route logs through OTel
    

    UseAzureMonitor() wires up the exporters and common instrumentation for ASP.NET Core and .NET so that logs, metrics, and traces share the same trace/activity identifiers and are sent to Application Insights.

    1. Getting the correlation ID into console/log stream output

    The correlation identifier in OpenTelemetry is the current Activity’s trace ID and span ID. When builder.Logging.AddOpenTelemetry is used, log records are correlated with the current Activity automatically. To see those IDs in console output:

    • Configure the console sink (Serilog or built‑in logging) to include the TraceId and SpanId that OpenTelemetry attaches to log scopes.
    • Because all logging is routed through OpenTelemetry, the same IDs that appear in Application Insights will be present in the log record metadata and can be rendered in the console/file format string.

    This way, the “correlation ID” seen in Azure (trace ID) is the same value visible in console/log stream when needed.

    1. Generally accepted configuration for local development

    For local development where console/file logging is primary:

    • Keep the same OpenTelemetry setup (AddOpenTelemetry + UseAzureMonitor) but:
      • Use an Application Insights connection string from a dev resource via APPLICATIONINSIGHTS_CONNECTION_STRING in appsettings.Development.json or environment variables, as shown:
            "AzureMonitor": {
                "ConnectionString": "InstrumentationKey=12345678-abcd-abcd-abcd-12345678..."
            }
        
      • Or omit the connection string to disable export to Azure while still using OpenTelemetry locally.
    • Route all logging through OpenTelemetry (builder.Logging.AddOpenTelemetry) and configure console/file providers to include the trace/activity identifiers in the log format.

    This matches the Modern Web App guidance: use the Azure Monitor OpenTelemetry distro, route all logging through OpenTelemetry, and rely on the built‑in trace/activity identifiers for end‑to‑end correlation across HTTP, Service Bus, and other boundaries.


    References:

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.