DEV Community

Andrew Malkov
Andrew Malkov

Posted on

7 1

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...

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay