I'm trying to configure Serilog in a .NET 5 WebApi project.
I can't figure out why everyone is injecting Serilog in Program.cs
, and not in the Startup.cs
file like this:
public void ConfigureServices(IServiceCollection services)
{
// inject serilog logger.
services.AddScoped<ILogger>(factory =>
{
return new LoggerConfiguration()
.WriteTo.File(@"C:\mylogs\log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
});
services.AddControllers();
}
If you register Logger
in DI instead of configuring ILoggerFactory
, you're not guaranteed that all your dependencies will use the one you registered in the startup.cs
.
In the below issue, it's exactly what happened when OP configured that in the startup.cs
.Net Core EventLog - override unhandled exception are written to Application log