I’ve used the ubiquitous Log4Net for as long as I can remember. It was there in most projects already and new projects often sprung from the same teams who already knew the flavour and style of the particular way it had been configured. Besides, why not?
Fast forward to .net core and a whole raft of new featured that came as part of it, IOC, JSON, Configuration… Logging!
I’m always keen to use what’s in the box as much as possible when it comes to any toolsets (boxes?) I use, so these new features are a real boon for me — less NuGet, more “I already got!”. “Excellent,” I thought when I heard about how we could just use what was in .net core. Except… I mainly log to console and file, and there is no file logging as a sink in Microsoft’s logging.
Do a google search for ‘Log4Net alternative’ and Serilog comes up — one of the most downloaded packages in Nuget. Sounds good enough for me.
As with the other examples, I just wanted to get up an running in a basic way and use the latest tools to add console or file logging to a simple(console) application.
You can use the static Serilog.Log method to write logs but this is not something I like to do. A reference to the logger can be registered in the IOC container and DI’d as required — a better practice. The code example shows the former approach, but adding IOC is so easy and if you are logging you ought to be architecting your application well anyway. Right?
There are several packages we need to add to our project:
Serilog — adds Serilog
Serilog.Sinks.Console — add the ability to log to the console
Serilog.Sinks.File — add the ability to log to a file
Serilog.Sinks.Async — adds the ability to log asynchronously, in the config the the Async sink wraps any other sinks you wish to log asynchronously to.
Microsoft.Extensions.Configuration — to add ConfigurationBuilder
Microsoft.Extensions.Configuration.Json — to read from appsettings.json file and add the AddJsonFile extension method
Serilog.Settings.Configuration — for Serilog to read from the appsettings.json file
Microsoft.Extensions.DependencyInjection — Not strictly needed to log… but any serious application will use DI so its needed for this example I show how you can use the static instance
Microsoft.Extensions.Logging — to use DI and add the logging we need this to provide the AddLogging method on the ServiceCollection
Serilog.Extensions.Logging — gives AddSerilog method to add to IOC
SeriLog.Enrichers.Environment — added to enrich the logging with environmental information, see the Enrich section in the configuration file.
Add an appsettings.json file.
Points to note:
The WriteTo (sinks) array as an item named ‘Async’ which wraps the configuration to the Console File and File sinks.
The ‘Enrich’ section that lists the items from SeriLog.Enrichers.Environment that are available — we are using MachineName in the file logger’s outputTemplate
The ‘Properties’ section that is just for show and demonstrated how custom name-value pairs can be defined and used in the outputTemplate