1
  1. Working version:
private static void SetLogging(WebApplicationBuilder builder)
{
    Log.Logger = new LoggerConfiguration()
        .Enrich.FromLogContext()
        .MinimumLevel.Override("Microsoft.EntityFrameworkCore.Migrations", LogEventLevel.Warning)
        .MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", LogEventLevel.Warning)
        .WriteTo.Console()
        .WriteTo.LokiHttp(() => new LokiSinkConfiguration
        {
            LokiUrl = "http://localhost:3100",
            LogLabelProvider = new DefaultLogLabelProvider(
                new List<LokiLabel>
                {
                    new ("assemblyName", "WebApi")
                }),
            OutputTemplate = "[{Level:u3}] {Message:lj}{NewLine}{Exception}"
        })
        .CreateLogger();
}

Logging output works both on the console and in Grafana Loki.

  1. Not working version:
private static void SetLogging(WebApplicationBuilder builder)
{
    var logger = new LoggerConfiguration()
        .ReadFrom.Configuration(builder.Configuration)
        .Enrich.FromLogContext()
        .CreateLogger();

    builder.Logging.ClearProviders();
    builder.Logging.AddSerilog(logger);
}

appsettings.json:

{
    "Serilog": {
      "Using": ["Serilog.Sinks.Console", "Serilog.Sinks.Loki"],
      "MinimumLevel": {
        "Default": "Information",
        "Override": {
          "Microsoft.EntityFrameworkCore.Migrations": "Warning",
          "Microsoft.EntityFrameworkCore.Database.Command": "Warning"
        }
      },
      "WriteTo": [
        {
          "Name": "Console"
        },
        {
          "Name": "LokiHttp",
          "Args": {
            "serverUrl": "http://localhost:3100",
            "labels": [
              {
                "key": "Identity",
                "value": "WebApi"
              }
            ],
            "outputTemplate": "[{Level:u3}] {Message:lj}{NewLine}{Exception}",
            "restrictedToMinimumLevel": "Information"
          }
        }
      ],
      "Enrich": ["FromLogContext"]
    }
}

Logging output does not work both on the console and in Grafana Loki.

It is used .net8, as well as in all variants:

NuGet packages:

<ItemGroup>
  <PackageReference Include="Serilog" Version="4.3.0" />
  <PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
  <PackageReference Include="Serilog.Settings.Configuration" Version="8.0.4" />
  <PackageReference Include="Serilog.Sinks.Async" Version="2.1.0" />
  <PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
  <PackageReference Include="Serilog.Sinks.Loki" Version="4.0.0-beta3" />
</ItemGroup>

usings:

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.Loki;
using Serilog.Sinks.Loki.Labels;

Why doesn't the second option work? What is the error?

1 Answer 1

0
private static void SetLogging(WebApplicationBuilder builder)

Indicates you are using it with a web application.

  <PackageReference Include="Serilog" Version="4.3.0" />
  <PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
  <PackageReference Include="Serilog.Settings.Configuration" Version="8.0.4" />

Indicates you are NOT using the ASP Net Integration

<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />

https://www.nuget.org/packages/Serilog.AspNetCore

The web integration (may) sometimes/often/almost ever needs the Two Stage-Initialization with the Bootstrap Logger.

You may also end up not using builder.Configuration but rather create a preloaded configuration instance and pass it to the bootstrap logger.

https://github.com/serilog/serilog-aspnetcore?tab=readme-ov-file#two-stage-initialization

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.