0

I want to know if it's possible to set a max count of rolling files?

I want to do that so I don't need to create a job to clean up old log files. Logs for the last month would be just fine.

This is my current configuration

        private static IHostBuilder CreateHostBuilder(string[] args)
        {
            var logFile = Path.Combine(_logPath, $"{_appName}_.json");
            var logFileErrors = Path.Combine(_logPath, $"{_appName}_errors_.json");
            var logFileStartUp = Path.Combine(_logPath, $"{_appName}_startup.json");

            return Host.CreateDefaultBuilder(args)
                .UseSerilog((context, provider, loggerConfig) =>
                    loggerConfig
                        .ReadFrom.Configuration(context.Configuration)
                        .Enrich.FromLogContext()
                        .Enrich.WithMachineName()
                        .Enrich.WithProperty("Assembly", $"{_appName}")
                        .Enrich.WithProperty("Version", $"{_appVersion}")
                        .WriteTo.Console(theme: AnsiConsoleTheme.Code)
                        .WriteTo.Logger(lc => lc
                            .Filter.ByIncludingOnly(e =>
                                e.Level is LogEventLevel.Warning or LogEventLevel.Error or LogEventLevel.Fatal)
                            .WriteTo.File(new ElasticsearchJsonFormatter(), logFileErrors,
                                fileSizeLimitBytes: 10485760L, rollingInterval: RollingInterval.Month,
                                rollOnFileSizeLimit: true))
                        .WriteTo.Logger(lc => lc
                            .Filter.ByIncludingOnly(e => e.Level is LogEventLevel.Information).Filter
                            .ByIncludingOnly(Matching.WithProperty("RequestId"))
                            .WriteTo.File(new ElasticsearchJsonFormatter(), logFile, fileSizeLimitBytes: 10485760L,
                                rollingInterval: RollingInterval.Month, rollOnFileSizeLimit: true))
                        .WriteTo.Logger(lc => lc
                            .Filter.ByExcluding(Matching.WithProperty("RequestId"))
                            .WriteTo.File(new ElasticsearchJsonFormatter(), logFileStartUp)))
                .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
        }
Martin Andersen
  • 2,460
  • 2
  • 38
  • 66

1 Answers1

0

This turned out to be a very easy task. Use retainedFileCountLimit. Code below show how it's used.

        private static IHostBuilder CreateHostBuilder(string[] args)
        {
            var logFile = Path.Combine(_logPath, $"{_appName}_.json");
            var logFileErrors = Path.Combine(_logPath, $"{_appName}_errors_.json");
            var logFileStartUp = Path.Combine(_logPath, $"{_appName}_startup.json");

            return Host.CreateDefaultBuilder(args)
                .UseSerilog((context, provider, loggerConfig) =>
                    loggerConfig
                        .ReadFrom.Configuration(context.Configuration)
                        .Enrich.FromLogContext()
                        .Enrich.WithMachineName()
                        .Enrich.WithProperty("Assembly", $"{_appName}")
                        .Enrich.WithProperty("Version", $"{_appVersion}")
                        .WriteTo.Console(theme: AnsiConsoleTheme.Code)
                        .WriteTo.Logger(lc => lc
                            .Filter.ByIncludingOnly(e =>
                                e.Level is LogEventLevel.Warning or LogEventLevel.Error or LogEventLevel.Fatal)
                            .WriteTo.File(new ElasticsearchJsonFormatter(), logFileErrors,
                                fileSizeLimitBytes: 10485760L, rollingInterval: RollingInterval.Month,
                                rollOnFileSizeLimit: true, retainedFileCountLimit: 5))
                        .WriteTo.Logger(lc => lc
                            .Filter.ByIncludingOnly(e => e.Level is LogEventLevel.Information).Filter
                            .ByIncludingOnly(Matching.WithProperty("RequestId"))
                            .WriteTo.File(new ElasticsearchJsonFormatter(), logFile, fileSizeLimitBytes: 10485760L,
                                rollingInterval: RollingInterval.Month, rollOnFileSizeLimit: true, retainedFileCountLimit: 5))
                        .WriteTo.Logger(lc => lc
                            .Filter.ByExcluding(Matching.WithProperty("RequestId"))
                            .WriteTo.File(new ElasticsearchJsonFormatter(), logFileStartUp)))
                .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
        }
Martin Andersen
  • 2,460
  • 2
  • 38
  • 66