- 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.
- 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?