Fix FileLogger filePath
This commit is contained in:
parent
9eee6683fa
commit
dc9e9e705c
@ -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)
|
||||
|
@ -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)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user