Serilog, Open Telemetry and Azure App Insights

141 Views Asked by At

I'm using .net core (8) and have Serilog enabled and am using Open Telemetry to push traces to Azure App Insights. I'm using the correct format in Serilog:

Log.Information("test {@msg}", msg);

All is working but the 'Custom Dimensions' only show the message type and I don't see the structured log. I can serialize the model but that defeats the point of having structured logging in the first place. I've tried all sorts but nothing seems to change this. Here's how I've wired up Open Telemetry and Serilog.

services.AddOpenTelemetry()

            .ConfigureResource(builder => ConfigureResource(

                builder, 

                cfg.GetSection("Application:Name").Value ?? "", 

                cfg.GetSection("Application:Version").Value ?? ""))

            .WithTracing(x => x

                .AddSource(DiagnosticHeaders.DefaultListenerName)

                .AddAzureMonitorTraceExporter(o =>

                {

                    o.ConnectionString = cfg.GetSection("AzureServiceBus:ApplicationInsightsConnectionString").Value;

                }))

            .WithMetrics(x => x.AddMeter(InstrumentationOptions.MeterName)

                .AddAzureMonitorMetricExporter(o =>

                {

                    o.ConnectionString = cfg.GetSection("AzureServiceBus:ApplicationInsightsConnectionString").Value;

                }))

            .UseAzureMonitor();
Log.Logger = new LoggerConfiguration()

                    .MinimumLevel.Information()

                    .WriteTo.Console()

                    .Enrich.WithProperty("ApplicationName", cfg["Application:Name"])

                    .WriteTo.ApplicationInsights(

                        cfg["AzureServiceBus:ApplicationInsightsConnectionString"],

                        new TraceTelemetryConverter())

                    .CreateLogger();```
1

There are 1 best solutions below

0
Matt On

I think I've fixed this. I hadn't called UseSerilog. It was still using the built in MS logger. Now I can see structured logs!!!