2
0

Always create and use MemoryLogger

This commit is contained in:
glax 2023-05-21 02:10:32 +02:00
parent 554f6b4acc
commit fe52d2c3b5
2 changed files with 10 additions and 6 deletions

View File

@ -9,13 +9,12 @@ public class Logger : TextWriter
public enum LoggerType public enum LoggerType
{ {
FileLogger, FileLogger,
ConsoleLogger, ConsoleLogger
MemoryLogger
} }
private FileLogger? _fileLogger; private FileLogger? _fileLogger;
private FormattedConsoleLogger? _formattedConsoleLogger; private FormattedConsoleLogger? _formattedConsoleLogger;
private MemoryLogger? _memoryLogger; private MemoryLogger _memoryLogger;
private TextWriter? stdOut; private TextWriter? stdOut;
public Logger(LoggerType[] enabledLoggers, TextWriter? stdOut, Encoding? encoding, string? logFilePath) public Logger(LoggerType[] enabledLoggers, TextWriter? stdOut, Encoding? encoding, string? logFilePath)
@ -30,7 +29,7 @@ public class Logger : TextWriter
throw new ArgumentException($"logFilePath can not be null for LoggerType {LoggerType.FileLogger}"); throw new ArgumentException($"logFilePath can not be null for LoggerType {LoggerType.FileLogger}");
} }
_formattedConsoleLogger = enabledLoggers.Contains(LoggerType.ConsoleLogger) ? new FormattedConsoleLogger(null, encoding) : null; _formattedConsoleLogger = enabledLoggers.Contains(LoggerType.ConsoleLogger) ? new FormattedConsoleLogger(null, encoding) : null;
_memoryLogger = enabledLoggers.Contains(LoggerType.MemoryLogger) ? new MemoryLogger(null, encoding) : null; _memoryLogger = new MemoryLogger(null, encoding);
} }
public void WriteLine(string caller, string? value) public void WriteLine(string caller, string? value)
@ -46,9 +45,14 @@ public class Logger : TextWriter
return; return;
_fileLogger?.Write(caller, value); _fileLogger?.Write(caller, value);
_memoryLogger?.Write(caller, value);
_formattedConsoleLogger?.Write(caller, value); _formattedConsoleLogger?.Write(caller, value);
_memoryLogger.Write(caller, value);
stdOut?.Write(value); stdOut?.Write(value);
} }
public string[] Tail(uint? lines)
{
return _memoryLogger.Tail(lines);
}
} }

View File

@ -14,7 +14,7 @@ public static class Tranga_Cli
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
Logger logger = new(new[] { Logger.LoggerType.FileLogger, Logger.LoggerType.MemoryLogger }, null, null, Logger logger = new(new[] { Logger.LoggerType.FileLogger }, null, null,
Path.Join(Directory.GetCurrentDirectory(), $"log-{DateTime.Now:dd-M-yyyy-HH-mm-ss}.txt")); Path.Join(Directory.GetCurrentDirectory(), $"log-{DateTime.Now:dd-M-yyyy-HH-mm-ss}.txt"));
logger.WriteLine("Tranga_CLI", "Loading Settings."); logger.WriteLine("Tranga_CLI", "Loading Settings.");