Skip to content

Serilog alongside Azure Monitor OpenTelemetry #285

@SamueleFurnari

Description

@SamueleFurnari

Description
In an AspNet 8.0 project I configured Azure Monitor OpenTelemetry to make monitoring easy on Microsoft Azure cloud platform. At the same time I would like to use the flexibility of serilog to write formatted logs to console and file.

In the project I also use entity framework. If I disable Serilog, on Azure Application Insight I also find the queries towards the database logged. If I enable Serilog, I no longer see the traces.

On stack overflow I found this question, but by configuring serilog as indicated, I see the traces on app insights but I still do not see the sql entity framework queries.

Thanks a lot for any help!
Samuele

Reproduction

  1. Add Serilog configuration in app.settings:
"Serilog": {
  "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
  "WriteTo": [
    { "Name": "Console" },
    {
      "Name": "File",
      "Args": {
        "path": "/logs/log-.txt",
        "rollingInterval": "Day",
        "rollOnFileSizeLimit": true,
        "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
      }
    }
  ],
  "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
}
  1. Configure Serilog and Azure Monitor Telemetry as below.
var resourceAttributes = new Dictionary<string, object>
{
    { "service.name", "my-service" },
    { "service.namespace", "my-namespace" },
    { "service.instance.id", "my-instance" }
};

builder.Logging.ClearProviders();

builder.Services
    .AddOpenTelemetry()
    .UseAzureMonitor()
    .ConfigureResource(resourceBuilder => resourceBuilder.AddAttributes(resourceAttributes));

builder.Services.AddSerilog(
    (services, lc) => lc
        .ReadFrom.Configuration(builder.Configuration)
        .ReadFrom.Services(services)
        .Enrich.FromLogContext(), writeToProviders: true);

Expected behavior
I want to see traces logged on Console, File and Application Insight but with this configuration I see traces only on Console and File logging.

Relevant package, tooling and runtime versions
I'm using Azure.Monitor.OpenTelemetry.AspNetCore v. 1.2.0 and Serilog.AspNetCore v. 9.0.0
The .NET framework is v. 8.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions