Tuesday, May 13, 2014

Log objects using SeriLog

After using Enterprise Library Logging Application Block for a long time, it’s time to move on. At the moment I’m investigating SeriLog, a promising logging library for .NET.

From the site:

Why Serilog?

Like other logging libraries for .NET, Serilog provides diagnostic logging to files, the console, and so-on. It is easy to set up, has a clean API, and is portable between recent .NET platforms.

Unlike other logging libraries for .NET, Serilog is built with structured log data in mind. Parameters passed along with log messages are not destructively rendered into a text format. Instead, they're preserved as structured data that can be written in document form to a NoSQL data store.

One of the biggest advantages of SeriLog is the support for structured logging and the integration with multiple data stores(‘Sinks’ as they are called in SeriLog).

SeriLog allows you to log anonymous types. It will print out all properties of the type.

If you execute the following statement:

this is what is saved inside the log file:

2014-05-09 17:08:54.249 +02:00 [Information] In my bowl I have "{ FruitId = 1, FruitName = Test }"

But if you try to do the same thing for a normal class it doesn’t work.

If you execute the following statement:

this is what is saved inside the log file:

2014-05-09 17:09:35.009 +02:00 [Information] In my bowl I have "WebApplication1.Controllers.HomeController+Fruit"

It is still possible to log the properties instead, but you have to add a “@” inside the log statement before the log parameter:

1 comment:

Kiquenet said...

What's about LibLog ?

https://cmatskas.com/application-logging-with-liblog/

and Elastic Search, Kibana and Serilog
https://fransiscuss.com/2015/10/20/logging-in-net-elastic-search-kibana-and-serilog/

Maybe better a full source code sample real application using Serilog with good patterns and practices ?