Back to list

Serilog - Structured Logging

Lv.5961@mukitaro10 playsDec 31, 2025

Serilog configuration with multiple sinks, enrichers, and structured logging for .NET applications.

preview.csharp
C#
1public class Program
2{
3 public static void Main(string[] args)
4 {
5 Log.Logger = new LoggerConfiguration()
6 .MinimumLevel.Debug()
7 .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
8 .Enrich.FromLogContext()
9 .Enrich.WithMachineName()
10 .Enrich.WithThreadId()
11 .WriteTo.Console(outputTemplate:
12 "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
13 .WriteTo.File("logs/app-.log",
14 rollingInterval: RollingInterval.Day,
15 retainedFileCountLimit: 7)
16 .CreateLogger();
17
18 try
19 {
20 Log.Information("Application starting up");
21 CreateHostBuilder(args).Build().Run();
22 }
23 catch (Exception ex)
24 {
25 Log.Fatal(ex, "Application failed to start");
26 }
27 finally
28 {
29 Log.CloseAndFlush();
30 }
31 }
32}

Custom problems are not included in rankings