I am using Serilog.Sinks.File in .NET Core 5 and it is working fine.
In .NET Core 5, I am using the subsequent configuration, startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.CreateLogger();
loggerFactory.AddSerilog();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
}
And appsettings.json file has
"Serilog": {
"MinimumLevel": "Information",
"Override": {
"Microsoft": "Warning"
},
"Using": [
"Serilog.Sinks.File"
],
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"pathFormat": "C:\\logs\\myapi\\log-{Date}.txt"
}
}
]
}
The Nuget package used
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
Now, I am trying to achieve the same thing in .NET Core 6 application. I have kept the appsettings.json file as is it and used the code in Program.cs
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Configuration)
.Enrich.FromLogContext()
.CreateLogger();
builder.Logging.ClearProviders();
builder.Logging.AddSerilog(logger);
But the application is not creating log files. What am I missing?
Try installing Serilog.AspNetCore
and then add then change builder.Logging.AddSerilog(logger);
to:
builder.Host.UseSerilog();