Debugging Serilog

When you play with different configurations for Serilog or try to add another sink you may run into problems. If you are out of luck your application crashes without the slightest notice – Serilog silently eats it’s own errors. In such cases you have to dig a little deeper into Serilog to find out what’s going on.

This post is part of the Improve Your Log Messages series. You can find the other parts here:

Luckily for us the developers behind Serilog are used to write meaningful log messages. We therefore don’t need to use the debugger and can solve our problems by reading the log messages. But those messages normally don’t end up in our sink. This behaviour is much appreciated for all the time Serilog works, but not when it crashes. In this situation we need to activate its self-logging sink.

When we want the log messages of Serilog to be written to the console we only need this single line of code:

The safest place to put this line is before you start with the set-up of Serilog. If you put the line after the LoggerConfiguration and an error happens there you will not get any output. Your call to SelfLog is never reached and therefore doesn’t have any effect.

Should you forget to call Initialize() on your RavenDB datastore you will get this output:

2014-09-20T20:32:00 Exception while emitting periodic batch from Serilog.Sinks.RavenDB.RavenDBSink: System.AggregateException: …..
—> System.InvalidOperationException: You cannot open a session or access the database commands before initializing the document store. Did you forget calling Initialize()?
at Raven.Client.DocumentStoreBase.AssertInitialized()

With log messages like these the whole problem solving part is a no-brainer.

 

Next

With Serilog and Seq we are able to understand the log messages that our application creates. However, I think we should go even further and reach the next level of data analysis. Next week we will look at Elasticsearch to get all set up to use Kibana for even more insights into our applications.

 
Update on 2014/09/26: Added a clarification on silently eaten errors by Serilog.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.