SeriLog Setting's Tips 2022

Installation

  1. Use NuGet to search for and install the Serilog.AspNetCore package image

  2. Add configuration in Program.cs image

    1
    2
    3
    4
    5
    6
    7
    
    Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
            .MinimumLevel.Override("System", LogEventLevel.Warning)//Use MinimumLevel to set the LogLevel. If the source is from Microsoft or system messages, then the LogLevel is set to Warning
            .WriteTo.Console(new RenderedCompactJsonFormatter())
            .WriteTo.File(new CompactJsonFormatter().ToString())//Output templates, output to Console and File
            .Enrich.FromLogContext()
            .CreateLogger();

    CompactJsonFormatter will preserve message templates, properties, and formatting information so that rendered messages can be created later. When JSON is intended to be used in environments without message template rendering, RenderedCompactJsonFormatter can be used instead.

    image loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration) : Loading currentConfiguration

  3. Add configuration in appsettings.json image

    • Microsoft.Hosting.Lifetime : Notifications for application lifecycle events
    • Microsoft.EntityFrameworkCore.Database.Command : Logging of automatically generated SQL commands by EF Core
  4. Startup.cs setting image

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    
    app.UseSerilogRequestLogging(options =>
             {
                 // Customize the message template
                 options.MessageTemplate = "Handled {RequestPath}";
    
                 // Emit debug-level events instead of the defaults
                 options.GetLevel = (httpContext, elapsed, ex) => LogEventLevel.Debug;
    
                 // Attach additional properties to the request completion event
                 options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
                 {
                     diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
                     diagnosticContext.Set("RequestScheme", httpContext.Request.Scheme);
                 };
             });

Usage

  1. Add logging where needed

    1. First, use Microsoft.Extensions.Logging
    2. Add a constructor image
    3. Add log entries where logging is needed image

      Customize the log message level image

  2. Since configurations are added in Program.cs and appsettings.json, all system operations and database reads will be logged. image

    image

Reference materials

  1. Github - serilog-aspnetcore
  2. Github - Serilog.Formatting.Compact
  3. Microsoft - .NET Core 與 ASP.NET Core 中的記錄
  4. WilL Blog - ASP.NET Core 如何紀錄 Entity Framework Core 5.0 自動產生的 SQL 命令
  5. m@rcus 學習筆記 - [NETCore] 結構化日誌 Serilog - 配置設定
  6. C# Corner - How To Implement Logging Using Serilog In ASP.NET Core 5.0 Application With Database
0%