How should Serilog be configured if you want to use it within NetCore and a generic Host
(or IHost
), not a WebHost
or something else?
There are the two options I found:
.UseSerilog()
from the Serilog.Extensions.Hosting
NuGet package:var host = Host.CreateDefaultBuilder()
.UseSerilog()
.UseSystemd()
.UseWindowsService()
.Build();
.ConfigureLogging()
and .AddSerilog()
from the Serilog
NuGet package:.ConfigureLogging(
logging =>
{
logging.AddSerilog();
});
So there are two questions now:
From Serilog.AspNetCore
(version 5.0.0) on, the second option is marked as deprecated and the generic host option (Option 1) should be used. For example, with a web host builder:
private static IHostBuilder CreateHostBuilder(string[] args, string currentLocation) =>
Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(
webBuilder =>
{
webBuilder.UseContentRoot(currentLocation);
webBuilder.UseStartup<Startup>();
})
.UseSerilog();