admin管理员组

文章数量:1123344

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?

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?

Share Improve this question asked 12 hours ago mrkmrk 213 bronze badges 1
  • Please share your configuration file. – Harshitha Commented 8 hours ago
Add a comment  | 

1 Answer 1

Reset to default 1

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);

本文标签: How to send logs to Application Insights with Serilog and Azure Monitor OpenTelemetryStack Overflow