Related Posts

Share This

Configuring Serilog from json

The past weeks I’ve been working with creating a service API in Asp.Net 5 and .Net Core, exciting stuff.

We’re running this in a docker container configured from the base Asp Net containers so it’s all easy to deploy. Well almost. you have to have logging right? And the file structure isn’t quite the same when debugging on a windows machine and running it in a *nix based environment. So what to do? File based configuration of course.

We’re using Serilog for logging, hooking this into the Asp.Net 5 logging structure. Serilog is a very mature framework and has loads of plugins, only they’re not all ported to .Net Core and in general quite a few things are missing. The Serilog team has been kind enough to open a little entryway for us if we do want to feed it configuration. This is the ReadFrom property on the logger class.

I simply read key-value pairs from my json file and loaded these into the configuration using ReadFrom.KeyValuePairs and voila. I wanted to note this down for future reference though because it’s not obvious quite how this is done and what exactly it wants as key value pairs, a few things to notice is that you have to supply the using for libraries such as RollingFile or ColoredConsole which in .Net Core are shipped as sinks. Also, the example on the Serilog wiki only mentions reading from an AppSettings xml and the format is slightly different since you’re feeding it values directly.

So to round off, here’s an example of how I define and load the configuration:


Reading configuration

Adding Serilog to Asp.Net 5