I’ve been struggling trying to get Serilog
to send telemetry over to the dotnet Aspire
dashboard. Once the Aspire
dashboard opens, the Structured logs
tab is always empty.
If I comment out the Serilog
configuration in Program.cs
(thus using the default ILogger
) then I start seeing structured logs on the dashboard.
There are currently two issues on the Aspire
repo which didn’t seem to help me:
- Serilog for Dotnet Aspire
- duplicate service name when using OTLP logging
This is where our Serilog
configuration stands at the moment:
// This extension is auto generated when adding Aspire. More on that below..
builder.AddServiceDefaults();
builder.Host.UseSerilog((hostContext, services, loggerConfiguration) => loggerConfiguration
.ReadFrom.Configuration(builder.Configuration)
.Enrich.FromLogContext()
.Enrich.WithPropertiesMasking(builder.Configuration)
.Enrich.WithProperty("ServiceName", DaprConstants.API_SERVICE_NAME)
.Enrich.WithThreadId()
.Enrich.WithCorrelationIdHeader("X-Correlation-ID")
.WriteTo.ApplicationInsights
(
new TelemetryConfiguration() { ConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"] },
TelemetryConverter.Traces
)
.WriteTo.OpenTelemetry(opts =>
{
opts.Endpoint = builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]!;
opts.ResourceAttributes.Add("service.name", builder.Configuration["OTEL_SERVICE_NAME"] ?? "Unknown");
})
);
If you’re unfamiliar with some of those environment variables you can read more about it here: .NET Aspire telemetry
From the GitHub
issues I linked to above I’ve also modified the Extensions
within the ServiceDefaults
project that gets auto generated when you add Aspire
to your application. I should note that I also tried keeping all the configuration in the Extensions
class
the same as well.
public static class Extension
{
public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder)
{
builder.ConfigureOpenTelemetry();
/* Other configuration... */
return builder;
}
public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder)
{
// Tried commenting this out.
/*builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});*/
builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddRuntimeInstrumentation();
})
.WithTracing(tracing =>
{
if (builder.Environment.IsDevelopment())
{
// We want to view all traces in development
tracing.SetSampler(new AlwaysOnSampler());
}
tracing.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation();
});
builder.AddOpenTelemetryExporters();
return builder;
}
private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder)
{
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
if (useOtlpExporter)
{
// In one of the GitHub issues David Fowler suggested commenting this line out.
//builder.Services.Configure<OpenTelemetryLoggerOptions>(logging => logging.AddOtlpExporter());
builder.Services.ConfigureOpenTelemetryMeterProvider(metrics => metrics.AddOtlpExporter());
builder.Services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddOtlpExporter());
}
if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
{
builder.Services.AddOpenTelemetry()
.UseAzureMonitor();
}
return builder;
}
}
I didn’t show the AppHost
here as I didn’t think it mattered to the aforementioned issue.
Has anyone who is currently trying out Aspire
with Serilog
been able to get this to work correctly?
If so, any pointers?