Set up Serilog in .NET 6 as a logging provider

Let's set up Serilog as Logging Provider in the native logging system in .NET so you can use the Microsoft ILogger interface.

Serilog is a robust API for logging with many configurations and sinks (outputs) and it is straightforward to get started in any .NET version.

With .NET 6 the way we used to configure Serilog as a logging provider in .NET 5 is gone (no more, no way sir, no no, goodbye), it's no longer there.
Bootstrapping a .NET 6 application is different from the older version but still pretty easy.

Like many other libraries for .NET, Serilog provides diagnostic logging to files, the console, and elsewhere. It is easy to set up, has a clean API, and is portable between recent .NET platforms. Unlike other logging libraries, Serilog is built with powerful structured event data in mind.

Words from https://serilog.net/

Let's set up Serilog as Logging Provider in the native logging system in .NET so you can use the Microsoft ILogger interface.

namespace Microsoft.Extensions.Logging
{
    public interface ILogger<out TCategoryName> : ILogger
    {
    }
}
Microsoft ILogger<T> interface

Enough with the words - show me the code

First, add Serilog dependencies packages to our project.

dotnet add package Serilog
dotnet add package Serilog.Extensions.Hosting
dotnet add package Serilog.Sinks.Console

Them in the Program.cs add these code changes.

using Serilog;

//create the logger and setup your sinks, filters and properties
Log.Logger = new LoggerConfiguration()
    .WriteTo.Console()
    .CreateBootstrapLogger();

var builder = WebApplication.CreateBuilder();
//after create the builder - UseSerilog

builder.Host.UseSerilog();

//redacted code
Program implementation with Serilog

With the changes made until here, we can inject the ILogger interface and use Serilog as a log provider.

💡
Don't forget to become a member and join our newsletter.

Brief Explanation

Set up the global variable Serilog.Logger with the sink Console and bootstrap a logger.

//create the logger and setup your sinks, filters and properties
Log.Logger = new LoggerConfiguration()
    .WriteTo.Console()
    .CreateBootstrapLogger();

Then we added Serilog as Logging Provider in the native logging system.

builder.Host.UseSerilog();

Output

Serilog Output Sample

Enabling Serilog's internal debug logging

If you are having any problems with Serilog, you can subscribe to its internal events and write them to your debug window or a console.

Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));Serilog.Debugging.SelfLog.Enable(Console.Error);
Please note that the internal logging will not write to any user-defined sinks.

Customize the output format of your Logs

Serilog allows you to customize the output templates of sinks. Such as which fields you include, their order, formats, etc.

Here is a simple example:

//create the logger and setup your sinks, filters and properties
Log.Logger = new LoggerConfiguration()
    .WriteTo.Console(outputTemplate:        
    	"[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"))
    .CreateBootstrapLogger();

The following fields can be used in a custom output template:

  • Exception
  • Level
  • Message
  • NewLine
  • Properties
  • Timestamp

❤️ Enjoy this article?

Forward to a friend and let them know.
Leave a comment with questions or improvements.