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
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);
. . .
That's it.
You can find more information and full code example here: Use Serilog with Minimal API in .NET 6
Top comments (1)
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...