How (not) to parameterize Serilog events

There’s one subtle and important difference though. The first parameter to Information(), here given the value "The time is {Time}" isn’t just an arbitrary string - it’s a Serilog message template.

You might be tempted occasionally to write events like:

Log.Information("The time is "+DateTime.Now);

Don’t do this. While never particularly good logging practise (the string concatenation occurs regardless of whether logging is enabled or not, wasting RAM and CPU cycles), with Serilog this is strongly considered an anti-pattern.

Why an anti-pattern?

The first example, using Serilog correctly, creates events that are logically like:

{"MessageTemplate":"The time is {Time}","Properties":{"Time":"2014-09-11T09:35.55.000"}}

The second, incorrect example will yield:

{"MessageTemplate":"The time is 2014-09-11T09:35.55.000","Properties":{}}

While you might not notice the difference initially when logging to the console or a text file, the first event is much more useful than the second:

Time is stored as a queryable property

All events of this type share the same message template, making the events queryable by type as well

Serilog is also optimised for creating well-formed events like the first one; while the second example won’t blow up, since each event has a unique “template” it will not take advantage of message template caching, requiring the message to be parsed each time. The extra junk in the message template cache will eventually cause the cache to flush, reducing logging performance throughout the app.

Be on the lookout…

There are a few ways your code can fall into this trap. Here’s (a simplification of) one that got me recently:

catch(Exceptionex){Log.Error(ex.Message);}

It’s a bit harder to spot the logging of an arbitrary string here, but like the broken example above, this will generate a potentially-unique template for every message.

(In case you’re wondering, the recommended way to log an exception with Serilog is:

catch(Exceptionex){Log.Error(ex,"Failed while trying to open the database");}

The exception will be attached to the event as a first-class property, and fully-rendered in both text and JSON… Easy!)

Event = Template + Properties

Just remember, a Serilog event isn’t a string - it’s the combination of a message template and zero-or-more properties.

Thinking this way will help you get more value out of Serilog as your application grows and you start to consider more advanced storage/querying options. Happy logging!