DEV Community

Andrew Malkov
Andrew Malkov

Posted on

Use Serilog with Minimal API in .NET 6

If you want to replace the standard logging with Serilog in Minimal API you have to do just a couple of steps.

At first, you have to add a reference to a NuGet package Serilog.AspNetCore:

dotnet add package Serilog.AspNetCore
Enter fullscreen mode Exit fullscreen mode

At second, you need to register Serilog as your logger in Program.cs:

. . .

// remove default logging providers
builder.Logging.ClearProviders();
// Serilog configuration        
var logger = new LoggerConfiguration()
    .WriteTo.Console()
    .CreateLogger();
// Register Serilog
builder.Logging.AddSerilog(logger);

. . .

Enter fullscreen mode Exit fullscreen mode

That's it.

You can find more information and full code example here: Use Serilog with Minimal API in .NET 6

Top comments (1)

Collapse
 
huntjason profile image
HuntJason

I had to add the following line to the bottom to actually get Serilog to log in a POST method in my minimal api:

Log.Logger = logger;

As per: stackoverflow.com/questions/712303...