diff --git a/Logging/FileLogger.cs b/Logging/FileLogger.cs index 522d3f1..b87e2b0 100644 --- a/Logging/FileLogger.cs +++ b/Logging/FileLogger.cs @@ -10,11 +10,12 @@ public class FileLogger : LoggerBase public FileLogger(string logFilePath, Encoding? encoding = null) : base (encoding) { this.logFilePath = logFilePath; + + DirectoryInfo dir = Directory.CreateDirectory(new FileInfo(logFilePath).DirectoryName!); //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); + for (int fileCount = dir.EnumerateFiles().Count(); fileCount > MaxNumberOfLogFiles - 1; fileCount--) //-1 because we create own logfile later + File.Delete(dir.EnumerateFiles().MinBy(file => file.LastWriteTime)!.FullName); } protected override void Write(LogMessage logMessage) diff --git a/Logging/Logger.cs b/Logging/Logger.cs index 46988d1..f34bfe1 100644 --- a/Logging/Logger.cs +++ b/Logging/Logger.cs @@ -23,14 +23,15 @@ public class Logger : TextWriter public Logger(LoggerType[] enabledLoggers, TextWriter? stdOut, Encoding? encoding, string? logFilePath) { this.Encoding = encoding ?? Encoding.UTF8; - if (enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is not null) - _fileLogger = new FileLogger(logFilePath, encoding); - else if(enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is null) + if(enabledLoggers.Contains(LoggerType.FileLogger) && (logFilePath is null || logFilePath == "")) { + DateTime now = DateTime.Now; logFilePath = Path.Join(LogDirectoryPath, - $"{DateTime.Now.ToShortDateString()} {DateTime.Now.ToShortTimeString()}.log"); + $"{now.ToShortDateString()}_{now.Hour}-{now.Minute}-{now.Second}.log"); _fileLogger = new FileLogger(logFilePath, encoding); - } + }else if (enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is not null) + _fileLogger = new FileLogger(logFilePath, encoding); + if (enabledLoggers.Contains(LoggerType.ConsoleLogger) && stdOut is not null) {