Serilog — simple .NET logging with fully-structured events (original) (raw)
Text formatting with a twist
Serilog message templates are a simple DSL extending .NET format strings. Parameters can be named, and their values are serialized as properties on the event for incredible searching and sorting flexibility:
var position = new { Latitude = 25, Longitude = 134 };var elapsedMs = 34; log.Information("Processed {@Position} in {Elapsed:000} ms.", position, elapsedMs);
This example records two properties, Position
andElapsed
along with the log event. The properties captured in the example, in JSON format, would appear like:
{"Position": {"Latitude": 25, "Longitude": 134}, "Elapsed": 34}
The @
operator in front of Position
tells Serilog to serialize the object passed in, rather than convert it using ToString()
.
The :000
segment following Elapsed
is a standard .NET format string that affects how the property is rendered. The console sink included with Serilog will display the above message as:
09:14:22 [Information] Processed { Latitude: 25, Longitude: 134 } in 034 ms.