Serilog Beginner

Benjamin Fan

2020/3/6

Existing Solution

  • Logging Application Block (Microsoft Enterprise Library)
  • Log4net
  • NLog
  • Elmah

Yet Another Log Library

  • March to a different tune?
  • Newer is better?
  • Is the icon prettier?

Why Serilog

  • Structured log message
  • Many sinks 
  • Flexible!

Structured Log Message

Simple, Scalar Values

var count = 456;
Log.Information("Retrieved {Count} records", count);
  • Booleans
  • Numerics
  • Strings
  • Temporals - DateTime, DateTimeOffset, TimeSpan
  • Others - Guid, Uri
  • Nullables - nullable versions of any of the types above

Structured Log Message

Collections

var fruit = new[] { "Apple", "Pear", "Orange" };
Log.Information("In my bowl I have {Fruit}", fruit);

Structured Log Message

Collections

var fruit = new Dictionary<string,int> {{ "Apple", 1}, { "Pear", 5 }};
Log.Information("In my bowl I have {Fruit}", fruit);

Structured Log Message

Objects

var position = new { Latitude = 25, Longitude = 134 };
Log.Logger.Warning("Processed {@Position}", position);

// 02:34:23 [AppId:{ Id = 1, Name = BenBen } WRN] Processed {"Latitude": 25, "Longitude": 134}

Many Sinks

There are so many sinks you can find here:

https://github.com/serilog/serilog/wiki/Provided-Sinks

Flexible

  • You can configuration settings via XML/JSON/Code
  • You can use sub-loggers
  • Sub-loggers + Sinks = Superpower

Reference

Made with Slides.com