Super Simple Serilog Example

David Horth
4 min readJan 11, 2022

Here is a simple speed guide to implementing Serilog advanced tools into your application. The goal of this document is not to provide a deep dive into Serilog, it is meant to provide a quick easy to understand guide to get you up and running with Serilog. For a more detailed description of Serilog I recommend the serilog github repository https://github.com/serilog. Serilog is a great tool and works well with all types of applications, Web, Console, Desktop, etc

Required packages

This is the list of packages I use in my application, but again refer to Serilog they have a wide variety of sinks available for all kinds of different uses. As always check for the latest versions

Load Serilog

This is where you load the logger, here I show it here in a static function called InitializeConsole, but it could just as easy be the first few lines of you program.

  • Step one is to get the IConfiguration, in the above is the snippet I load the available appsettings into my IConfiguration. Notice that the only required file is the appsettings.json, the rest are optional.
  • Step Two is loading Serilog using our configuration file.

Notice the I have included two Enrich statements in by base load function. Enrich.WithProperty - Used as an example but in this case I was every log line to have a available property call ApplicationContext, which I set to the running applications name. For me this is valuable because I aggregate events from a number of different application into my self hosted Seq site. The second Enrich.FromLogContext allows me to dynamically add additional properties to the log event at the time of logging. For example

Serilog Configuration File

This is a snippet out of the appsettings.json, In practical use I comment out the WriteTo's that do not make sense in my application. For instance a console app, I would only keep Console and maybe File depending on how the application is used. If I run the console application in an unattended mode then I would add in either the file or seq sinks. Bottom line is I prefer to setup and configure my logger in the appsettings, rather than the direct code base. It provides me with the flexibility and extensibility that I am looking for in a log tool.

Console

Write output to the console. Color formatting is applied by adding the theme

​ "theme": "Serilog.Sinks.SystemConsole.Themes.SystemConsoleTheme::Literate, Serilog.Sinks.Console"

Visual Studio Output window

Output log to the visual studio output window

Text File Log

Write to a standard text file. In this example I am using a output template to set the format of the line written to the log file. For more details on setting the output format see https://github.com/serilog/serilog-expressions

Rolling Json File

Write to a rolling date stamped file, I formatting this as json for this example, but it would be just as easy to roll a standard text file.

Seq

See datalush Seq for more information on the host

Can’t recommend this tool enough

Implementation

Log.Logger.Information("All done!  Press any key to exit");

Super easy to log, Log is the static entry point for logger, and is guaranteed not to throw an exception.

Good luck and for a real world example(s) take a look at my github projects

or

--

--