Skip to content

DRNJ

Light at the end of the Technology Tunnel

  • Home
  • About
  • Contact
DRNJ

Serilog in .Net Core 6

The Problem

I wanted to add Serilog to a WebAPI using .Net Core 6. The “startup” has changed from .Net Core 3 and the “wiring” up of services so it wasn’t clear to me how to get it all working.

The Solution

After some head scratching and searching I work out that you needed from this article and this article (thanks to the authers)

So I created a method

protected void ConfigureLogging(WebApplicationBuilder appBuilder)
{
appBuilder.Host.UseSerilog((ctx, lc) => lc
.ReadFrom.Configuration(ctx.Configuration)
.WriteTo.Console()
);

}

This method is called from the program.cs “startup” code.

Placed the following in appsetttings.json

{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"DRNJ": "Debug",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Filter": [
{
"Name": "ByExcluding",
"Args": {
"expression": "@mt = 'An unhandled exception has occurred while executing the request.'"
}
}
],
"WriteTo": [
{ "Name": "Console" },
{
"Name": "File",
"Args": {
"path": "c:\\temp\\log\\apilog-.txt",
"rollingInterval": "Day"
}
}
]
},
"AllowedHosts": "*"
}

 

And, voila, it all worked

 

.NET Core

Idealist by NewMediaThemes