Serilog is a logging library that can easily be built in into a classical .Net or Core application.
Installing and using Serilog is really straightforward and you can log to different storage types very easily as well with the so-called sinks.
Installation
Installing Serilog is as simple as installing the Serilog nuget package:
PM> Install-Package Serilog
Setup
The setup is very straightforward as well. An ILogger
is created using LoggerConfiguration
. This is normally done once at Startup and then used across all the other classes.
var log = new LoggerConfiguration()
.WriteTo.ColoredConsole()
.CreateLogger();
Then you can use the log variable like:
log.Information("Hello, Serilog!");
Sinks
Sinks generally record log events to some external representation, typically the console, a file or data store. Serilog sinks are distributed via NuGet packages. You can see a list of sinks by clicking here.
Sinks are similar to targets in NLog.
Configuration
You can configure Serilog in three different ways.
- Programmatically
- Using the web.config or appsettings file
- A mix of both
Programmatically
Sinks can be configured using the WriteTo
configuration object.
Log.Logger = new LoggerConfiguration()
.WriteTo.LiterateConsole()
.CreateLogger();
Log.Information("Ah, there you are!");
As expected this log would write to the console.
Multiple sinks can be configured by chaining them together.
Using a config file
To configure Serilog through a config file, first you need to add the nuget package:
PM> Install-Package Serilog.Settings.AppSettings
and use the ReadFrom.AppSettings() extension method:
Log.Logger = new LoggerConfiguration() .ReadFrom.AppSettings() ... // Other configuration here, then .CreateLogger()
To configure the logger, an <appSettings>
element should be included in the program’s App.config or Web.config file.
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="serilog:minimum-level" value="Verbose" /> <!-- More settings here -->
for more config setting see here.
A mix of both
You can mix and match XML and code-based configuration, but each sink must be configured either using XML or in code – sinks added in code can’t be modified via app settings.
Using an IOC container
Serilog can be added through DI in several ways to your code. If you’re using .Net Core one that I found particularly good is by integrating Serilog with the Microsoft.Extensions.Logging library by using the Serilog provider for that library.
To do this you need to install the Serilog.Extensions.Logging
PM> Install-Package Serilog.Extensions.Logging
And then you can just add Serilog to the pipeline:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory, IApplicationLifetime appLifetime) { loggerfactory.AddSerilog();
And then simply add the Microsoft extensions logging ILogger to the ctor of the class you want to add logs to:
public class MyClass{ public MyClass(ILogger<MyClass> logger){ logger.LogInformation("this is a log with a MyCLass source context!"); } ... }
Testing
Unit testing classes that use Serilog is really simple since if no configuration to Serilog is provided, it automatically creates a SilentLogger that does nothing, and by doing so it does not mess with your tests.
If you use Serilog with Microsoft extensions logging, then you will need to add the following code to register with autofac (if you use autofac) so that Serilog is correctly configured.
builder.RegisterInstance(new LoggerFactory().AddSerilog()).As<ILoggerFactory>(); builder.RegisterGeneric(typeof(Logger<>)).As(typeof(ILogger<>));
You should be then ready to go.
Conclusion
This post is just a scratch in the surface to what Serilog can do. But I think Serilog is doing some solid steps towards his future. Namely, the work done with sinks to make sure you can log to most storages out there. In this respect, Serilog is being more proactive than NLog. For instance logging to Elasticsearch is a real breeze with the Elasticsearch sink, but it’s way harder to do with NLog, the reason being that there are no good or up to date target libraries to work with NLog, at least at the time of writing of this post.