← ClaudeAtlas

logginglisted

Serilog structured logging for ASP.NET Core — setup, message templates, LogContext enrichment, request logging middleware, and log level guidelines.
zdanovichnick/dotnet-pilot · ★ 3 · AI & Automation · score 76
Install: claude install-skill zdanovichnick/dotnet-pilot
# Serilog Structured Logging Reference material for adding and using Serilog in ASP.NET Core. Used by `dnp-architect` and `dnp-tdd-developer-hard`. ## NuGet Packages ```xml <PackageReference Include="Serilog.AspNetCore" Version="8.*" /> <PackageReference Include="Serilog.Enrichers.Environment" Version="3.*" /> <PackageReference Include="Serilog.Enrichers.Thread" Version="4.*" /> <PackageReference Include="Serilog.Formatting.Compact" Version="3.*" /> <!-- Add sinks as needed: --> <PackageReference Include="Serilog.Sinks.OpenTelemetry" Version="4.*" /> ``` ## Setup — Program.cs Replace the default ASP.NET Core logging before `builder.Build()`: ```csharp builder.Host.UseSerilog((context, config) => config .ReadFrom.Configuration(context.Configuration) .Enrich.FromLogContext() .Enrich.WithMachineName() .Enrich.WithThreadId() .WriteTo.Console(new CompactJsonFormatter()) .WriteTo.OpenTelemetry()); // swap/add sinks as needed ``` For early startup errors, bootstrap a minimal logger before the host is built: ```csharp Log.Logger = new LoggerConfiguration() .WriteTo.Console() .CreateBootstrapLogger(); try { var builder = WebApplication.CreateBuilder(args); builder.Host.UseSerilog((ctx, cfg) => cfg.ReadFrom.Configuration(ctx.Configuration)...); // ... } catch (Exception ex) { Log.Fatal(ex, "Application startup failed."); } finally { Log.CloseAndFlush(); } ``` ## appsettings.json Configuration ```j