Added logfile-count limit
This commit is contained in:
parent
5dee13c402
commit
e24652b83e
@ -6,10 +6,16 @@ namespace Logging;
|
|||||||
public class FileLogger : LoggerBase
|
public class FileLogger : LoggerBase
|
||||||
{
|
{
|
||||||
private string logFilePath { get; }
|
private string logFilePath { get; }
|
||||||
|
private const int MaxNumberOfLogFiles = 5;
|
||||||
|
|
||||||
public FileLogger(string logFilePath, TextWriter? stdOut, Encoding? encoding = null) : base (stdOut, encoding)
|
public FileLogger(string logFilePath, TextWriter? stdOut, Encoding? encoding = null) : base (stdOut, encoding)
|
||||||
{
|
{
|
||||||
this.logFilePath = logFilePath;
|
this.logFilePath = logFilePath;
|
||||||
|
|
||||||
|
//Remove oldest logfile if more than MaxNumberOfLogFiles
|
||||||
|
string parentFolderPath = Path.GetDirectoryName(logFilePath)!;
|
||||||
|
for (int fileCount = new DirectoryInfo(parentFolderPath).EnumerateFiles().Count(); fileCount > MaxNumberOfLogFiles - 1; fileCount--) //-1 because we create own logfile later
|
||||||
|
File.Delete(new DirectoryInfo(parentFolderPath).EnumerateFiles().MinBy(file => file.LastWriteTime)!.FullName);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Write(LogMessage logMessage)
|
protected override void Write(LogMessage logMessage)
|
||||||
|
Loading…
Reference in New Issue
Block a user