Fix FileLogger filePath
This commit is contained in:
parent
9eee6683fa
commit
dc9e9e705c
@ -11,10 +11,11 @@ public class FileLogger : LoggerBase
|
|||||||
{
|
{
|
||||||
this.logFilePath = logFilePath;
|
this.logFilePath = logFilePath;
|
||||||
|
|
||||||
|
DirectoryInfo dir = Directory.CreateDirectory(new FileInfo(logFilePath).DirectoryName!);
|
||||||
|
|
||||||
//Remove oldest logfile if more than MaxNumberOfLogFiles
|
//Remove oldest logfile if more than MaxNumberOfLogFiles
|
||||||
string parentFolderPath = Path.GetDirectoryName(logFilePath)!;
|
for (int fileCount = dir.EnumerateFiles().Count(); fileCount > MaxNumberOfLogFiles - 1; fileCount--) //-1 because we create own logfile later
|
||||||
for (int fileCount = new DirectoryInfo(parentFolderPath).EnumerateFiles().Count(); fileCount > MaxNumberOfLogFiles - 1; fileCount--) //-1 because we create own logfile later
|
File.Delete(dir.EnumerateFiles().MinBy(file => file.LastWriteTime)!.FullName);
|
||||||
File.Delete(new DirectoryInfo(parentFolderPath).EnumerateFiles().MinBy(file => file.LastWriteTime)!.FullName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Write(LogMessage logMessage)
|
protected override void Write(LogMessage logMessage)
|
||||||
|
@ -23,14 +23,15 @@ public class Logger : TextWriter
|
|||||||
public Logger(LoggerType[] enabledLoggers, TextWriter? stdOut, Encoding? encoding, string? logFilePath)
|
public Logger(LoggerType[] enabledLoggers, TextWriter? stdOut, Encoding? encoding, string? logFilePath)
|
||||||
{
|
{
|
||||||
this.Encoding = encoding ?? Encoding.UTF8;
|
this.Encoding = encoding ?? Encoding.UTF8;
|
||||||
if (enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is not null)
|
if(enabledLoggers.Contains(LoggerType.FileLogger) && (logFilePath is null || logFilePath == ""))
|
||||||
_fileLogger = new FileLogger(logFilePath, encoding);
|
|
||||||
else if(enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is null)
|
|
||||||
{
|
{
|
||||||
|
DateTime now = DateTime.Now;
|
||||||
logFilePath = Path.Join(LogDirectoryPath,
|
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);
|
_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)
|
if (enabledLoggers.Contains(LoggerType.ConsoleLogger) && stdOut is not null)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user