Serilog - mcbride-clint/DeveloperCurriculum GitHub Wiki

Serilog is a modern 3rd party logging framework that has many of the features of older frameworks as well as support for structured data logging that can make logs easier to index and search. Structured logging stores the string message as well as the parameters as a json object that can be indexed and searched.

When using Microsoft.Extensions.Logging, you can use Serilog.Extensions.Logging(.Net Framework) or Serilog.AspNetCore(.Net Core) to easily integrate Serilog into the ILogger system.


  public void ConfigureServices(IServiceCollection services)
  {
      services.AddLogging(loggingBuilder =>
      	loggingBuilder.AddSerilog(dispose: true));
      
      // Other services ...
  }

Once you have the basic Serilog added, you can begin to configure with Enrichers and Sinks. Enrichers are additional fields that can be sent along with every log message such as Machine Name or User Name without having to include them manually. Sinks are logging targets, you can add as many as you want and configure each one individually.


Log.Logger = new LoggerConfiguration()
    .Enrich.With(new ThreadIdEnricher())
    .WriteTo.Console(
        outputTemplate: "{Timestamp:HH:mm} [{Level}] ({ThreadId}) {Message}{NewLine}{Exception}")
    .CreateLogger();

See Also