3

I want to see my ASP.NET Core application's logs in Application Insights.

I'm using the following packages:

  • Azure.Monitor.OpenTelemetry.AspNetCore
  • Serilog.AspNetCore
  • Serilog.Sinks.OpenTelemetry

This is the current setup:

builder.Services.AddOpenTelemetry()
    .UseAzureMonitor()
    .ConfigureResource(x =>
    {
        x.AddAttributes(new Dictionary<string, object> { { "service.name", "my-service" } });
    })
    .WithTracing();

builder.Services.AddSerilog((services, lc) => lc
    .ReadFrom.Configuration(builder.Configuration)
    .ReadFrom.Services(services)
    .Enrich.FromLogContext()
    .WriteTo.Console(builder.Environment.IsDevelopment()
        ? new RenderedCompactJsonFormatter()
        : new CompactJsonFormatter())
    .WriteTo.OpenTelemetry()
);

The connection string is passed via the environment variable APPLICATIONINSIGHTS_CONNECTION_STRING. I can see requests in the Application Insights dashboard, but no traces/logs. What's missing here?

1
  • Please share your configuration file. Commented Jan 10, 2025 at 17:57

2 Answers 2

9

UseAzureMonitor calls this under the hood:

builder.Services.AddLogging(
{
    logging.AddOpenTelemetry(builderOptions =>
    {
        builderOptions.IncludeFormattedMessage = true;
    });
});

which in turn registers an <ILoggerProvider, OpenTelemetryLoggerProvider>.

AddSerilog has a default option of writeToProviders = false. If you instead toggle it to true, logs will push through to Application Insights, but it tells you an equivalent Serilog sink should be used instead of writing to all providers.

After toggling that flag, you get double the logs, but I solved that with builder.Logging.ClearProviders();.

I don't know how the library authors intended this integration to look like, but this is my currently working setup:

builder.Logging.ClearProviders();

builder.Services.AddOpenTelemetry()
    .UseAzureMonitor()
    .ConfigureResource(x =>
    {
        x.AddAttributes(new Dictionary<string, object> { { "service.name", "my-service" } });
    });

builder.Services.AddSerilog((services, lc) => lc
    .ReadFrom.Configuration(builder.Configuration)
    .ReadFrom.Services(services)
    .Enrich.FromLogContext()
    .WriteTo.Console(builder.Environment.IsDevelopment()
        ? new RenderedCompactJsonFormatter()
        : new CompactJsonFormatter()), writeToProviders: true);
Sign up to request clarification or add additional context in comments.

3 Comments

I can't believe it's taken me this long to find the solution showing serilog and azure monitor. Everything existing still references the app insights libraries
Thank you this worked for me and my setup with the new Azure.Monitor.OpenTelemetry.AspNetCore package
Thanks, this code works for me as well. But I am not able to get it work using "UseSerilog()" also when I do a structured logging it adding a classname and not making it as actual structured log
-1

How about Serilog.Sinks.ApplicationInsights package?

It works very well for me.

2 Comments

Ivan Pavlov, please don't just post some tool or library as an answer. At least demonstrate how it solves the problem in the answer itself.
The package itself is the complete solution to the described problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.