Reputation: 81
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?
Upvotes: 2
Views: 238
Reputation: 81
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);
Upvotes: 5