My file still doesn't roll and I've followed all the setup instructions. I don't think this simple thing can be so hard to setup, can someone point out what I have wrong?
I have the latest of the following packages installed and my app runs on .Net Core 3.1:
- Serilog.AspNetCore
- Serilog.Enrichers.Environment
- Serilog.Enrichers.Thread
- Serilog.Settings.Configuration
- Serilog.Sinks.Console
- Serilog.Sinks.File
- Serilog.Sinks.RollingFileAlternate
This is my code in Program.cs
public class Program
{
// For Serilog docs refer to https://github.com/serilog/serilog-aspnetcore
public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
public static int Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.CreateLogger();
try
{
Log.Information("Starting web host");
CreateWebHostBuilder(args).Build().Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.UseSerilog()
//add configiration calling the method below
.ConfigureAppConfiguration(SetupConfiguration)
.UseStartup<Startup>();
}
public static void SetupConfiguration(WebHostBuilderContext ctx, IConfigurationBuilder builder)
{
//removing the default configuration options
builder.Sources.Clear();
builder.SetBasePath(ctx.HostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{ctx.HostingEnvironment.EnvironmentName}.json", optional: true)
.AddXmlFile("app.config");
}
}
And here's my appsettings.config part:
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "RollingFileAlternate",
"Args": { "path": "../Logs/MyLog_dev.log" },
"RollingInterval": "RollingInterval.Day",
"RetainedFileCountLimit": 20,
"fileSizeLimitBytes": 1000000,
"rollOnFileSizeLimit": true
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],