nreco / logging

Generic file logger for .NET Core (FileLoggerProvider) with minimal dependencies
MIT License
284 stars 56 forks source link

Log file cleanup #43

Closed Crypt32 closed 2 years ago

Crypt32 commented 2 years ago

First of all, thanks for this useful provider!

We use logs with names based on timestamps like this:

services.AddLogging(loggingBuilder => {
    loggingBuilder.AddFile("Logs\\app_{0:yyyy}-{0:MM}-{0:dd}.log", fileLoggerOpts => {
        fileLoggerOpts.FormatLogFileName = fName => String.Format(fName, DateTime.UtcNow);
        fileLoggerOpts.FileSizeLimitBytes = 1 * 1024 * 1024; // 1MB
        fileLoggerOpts.MaxRollingFiles = 10;
        fileLoggerOpts.UseUtcTimestamp = true;
        LogFilePath = fileLoggerOpts.FormatLogFileName.Invoke("Logs\\app_{0:yyyy}-{0:MM}-{0:dd}.log");
    });
})

However, we observed that older log files generated in previous app executions are not deleted. Is this by design and we need to write our own cleanup logic or it is because how we generate file names?

VitaliyMF commented 2 years ago

Take a look to this: https://github.com/nreco/logging/issues/25

In short, LoggerProvider doesn't perform any clean-ups because after an app restart it simply doesn't have a technical possibility to determine what log files were created previously. If you need this kind of logic you can implement it as part of your app (say, delete all files on the app start that older than 30 days or smth like that).

Crypt32 commented 2 years ago

Makes sense, thanks!