There are many log libraries on NuGet, but I would say the 3 most known ones are NLog, Log4Net and Serilog. This article will focus on Serilog with a simple example of how to start with it.

Why should you use Serilog?

Serilog is a powerful library allowing you to create logs for your application in a simple way. You can log in the Console, in log files and many other ways (cfr Sinks in the next chapter).

Serilog is compatible with .Net Framework, .Net Core and obviously .Net 5 and 6.

The configuration is pretty straightforward using a config file, the appSettings, or directly via C#.

What are Serilog Sinks?

Serilog Sinks are just different ways to output your logs. It could be in the Console, in a log file, SQL server, Seq, RabbitMQ, MongoDB, … Here is the complete list of Sinks for Serilog. All of these Sinks can be found on NuGet. So if you want to log in the Console, you will need Serilog.Sinks.Console, in a file: Serilog.Sinks.File, … But you don’t have to chose, you can combine them.

In this example, we will log in the Console and in a text file at the same time:

Log.Logger = new LoggerConfiguration() .WriteTo.Console() .WriteTo.File("log-.txt", rollingInterval: RollingInterval.Day) .CreateLogger(); Log.Information("Serilog has been initialized.");
Code language: C# (cs)

How to configure Serilog from the AppSettings.json?

Instead of configuring Serilog in C#, you could use the AppSettings or the Web.config file. To be able to do that, you will need to install the NuGet package Serilog.Settings.AppSettings.

Here is a basic config to put in the AppSettings.json:

{ "Serilog": { "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], "MinimumLevel": "Debug", "WriteTo": [ { "Name": "Console" }, { "Name": "File", "Args": { "path": "Logs/log.txt" } } ], "Properties": { "Application": "Serilog-Demo" } } }
Code language: C# (cs)

During the initialization of the application, you will still need to specify how Serilog has to be configured:

var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build(); Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(configuration) .CreateLogger(); Log.Information("Serilog has been initialized.");
Code language: C# (cs)

What are the basic dependencies needed for Serilog?

If you want to log like in this example in the Console and in a text file with the configuration in the AppSettings.json, you will need at the minimum these NuGet packages:

Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.FileExtensions
Microsoft.Extensions.Configuration.Json
Serilog
Serilog.Settings.AppSettings
Serilog.Settings.Configuration
Serilog.Sinks.Console
Serilog.Sinks.File

Serilog simple Console example.

using Microsoft.Extensions.Configuration; using Serilog; using System; using System.IO; namespace Serilog_Demo { class Program { static void Main(string[] args) { var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build(); Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(configuration) .CreateLogger(); ////Configure Serilog with C# //Log.Logger = new LoggerConfiguration() // .MinimumLevel.Debug() // .WriteTo.Console() // .WriteTo.File("log-.txt", rollingInterval: RollingInterval.Day) // .CreateLogger(); Log.Information("Serilog has been initialized."); try { Log.Debug("Here we will try to do a division per zero:"); var foo = 0; var bar = 3 / foo; } catch (Exception ex) { Log.Fatal(ex, "Division per zero occured."); } Log.Information("Closing Serilog at the end of the application."); Log.CloseAndFlush(); } } }
Code language: C# (cs)

The Console output will look like this:

serilog-console-sink

And the log file:

2021-10-03 17:18:38.804 +02:00 [INF] Serilog has been initialized. 2021-10-03 17:18:38.846 +02:00 [DBG] Here we will try to do a division per zero: 2021-10-03 17:18:38.872 +02:00 [FTL] Division per zero occured. System.DivideByZeroException: Attempted to divide by zero. at Serilog_Demo.Program.Main(String[] args) in D:\OneDrive\Dev\Projects\C#\Tests\Serilog-Demo\Serilog-Demo\Program.cs:line 34 2021-10-03 17:18:38.979 +02:00 [INF] Closing Serilog at the end of the application.
Code language: YAML (yaml)

How to use Serilog with Dependency Injection?

You can use the interface ILogger<T> from Microsoft.Extensions.Logging to do an abstraction of the logging library you are using. This is useful in case you would like to change it later on without changing the whole code.