RavenDB as a Sink for Serilog

As I explained in the last post, creating JSON documents with Serilog is easy. But to make something useful with the log messages we must be able to query them. When I think about storing JSON documents then RavenDB is the first system that comes to my mind.

RavenDB was the topic of many posts in this blog and is a very capable NoSQL solution to work with JSON documents. And even better, to use RavenDB with Serilog you only need another NuGet package.

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

 

Installing a Sink

Serilog needs a special sink to write to RavenDB. We don’t need much more than installing the official NuGet package. That will not only install the sink, but will fetch all the dependencies to RavenDB.Client in one single action:

Install NuGet package

 

Configure the Sink

If you know RavenDB then the configuration is straightforward. All you need is a document store that points to your database and an additional line in your call to LoggerConfiguration. Don’t forget to initialize your document store, otherwise you only log to the console or create an error…

All this code only modifies the configuration of your Logger-object to use RavenDB. The rest of your application can stay the same and doesn’t need to be modified. Serilog will write your log messages from now on to the specified RavenDB database:

Stored in RavenDB

 

Search your Messages

The documents representing your log messages are now in RavenDB and can be processed like every other JSON document. RavenDB doesn’t care if they are from Serilog or if you created them on your own.

As always you can use an index or a dynamic query to search for a specific message or collection of related messages. When you want to get all the messages created while order 123 was processed you can use this query:

Query Serilog message in RavenDB

 

Next

RavenDB is a great step forward to query your log messages. However, RavenDB is a general purpose NoSQL solution that has no particular knowledge of log messages. Therefore you need to write all the queries on your own. You can analyse them in every way you want, but even for the simplest task you need to put some effort into it.

Next time we will look at Seq. This system is built for analysing log messages and offers you some pre-defined queries.

4 thoughts on “RavenDB as a Sink for Serilog”

  1. Great post, but what makes RavenDB any better than Mongo or any of the other NoSQL databases that sinks exist for?

    Reply
    • Hi Ryan,
      I started with the sink for RavenDB because I blogged a lot about this NoSQL database already. Seq and Elasticsearch are 2 other sinks I cover in more depth, but so far I never tried the one for MongoDB. Which one to choose is more about your needs than the implementation quality – as far as I can tell they work all as expected.

      Reply
      • Thanks for replying. I was wondering if some had a reputation for ease of use, or stability, or things of that nature. We are currently using Seq with rolling files and am wondering whether what the gains would be to set up a NoSQL database in addition to that. I don’t predict having billions of rows, so simple files might meet our needs fine. Seq already has great querying ability, is there situations where you really need the NoSQL solution?

        Reply
        • So far I personally don’t need the NoSQL solution for the storage or the performance. However, I particularly like the way Kibana displays the messages and events. That alone was for me enough to try Elasticsearch (another NoSQL solution which I feature in today’s blog post). So far I prefer Kibana over the query mode of Seq, but this may change should Seq add more features in that part of the application.

          Reply

Leave a Comment

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