diff --git a/Logging/FileLogger.cs b/Logging/FileLogger.cs index 7c844b2..10fd0ec 100644 --- a/Logging/FileLogger.cs +++ b/Logging/FileLogger.cs @@ -1,5 +1,4 @@ using System.Text; -using System.Text.Json.Serialization; namespace Logging; @@ -8,7 +7,7 @@ public class FileLogger : LoggerBase 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, Encoding? encoding = null) : base (encoding) { this.logFilePath = logFilePath; @@ -22,11 +21,11 @@ public class FileLogger : LoggerBase { try { - File.AppendAllText(logFilePath, logMessage.ToString()); + File.AppendAllText(logFilePath, logMessage.formattedMessage); } - catch (Exception e) + catch (Exception) { - stdOut?.WriteLine(e); + // ignored } } } \ No newline at end of file diff --git a/Logging/FormattedConsoleLogger.cs b/Logging/FormattedConsoleLogger.cs index a3c6467..2e920fe 100644 --- a/Logging/FormattedConsoleLogger.cs +++ b/Logging/FormattedConsoleLogger.cs @@ -4,14 +4,14 @@ namespace Logging; public class FormattedConsoleLogger : LoggerBase { - - public FormattedConsoleLogger(TextWriter? stdOut, Encoding? encoding = null) : base(stdOut, encoding) + private readonly TextWriter _stdOut; + public FormattedConsoleLogger(TextWriter stdOut, Encoding? encoding = null) : base(encoding) { - + this._stdOut = stdOut; } protected override void Write(LogMessage message) { - //Nothing to do yet + this._stdOut.Write(message.formattedMessage); } } \ No newline at end of file diff --git a/Logging/LogMessage.cs b/Logging/LogMessage.cs new file mode 100644 index 0000000..56515c0 --- /dev/null +++ b/Logging/LogMessage.cs @@ -0,0 +1,23 @@ +namespace Logging; + +public class LogMessage +{ + public DateTime logTime { get; } + public string caller { get; } + public string value { get; } + public string formattedMessage => ToString(); + + public LogMessage(DateTime messageTime, string caller, string value) + { + this.logTime = messageTime; + this.caller = caller; + this.value = value; + } + + public override string ToString() + { + string dateTimeString = $"{logTime.ToShortDateString()} {logTime.ToLongTimeString()}.{logTime.Millisecond,-3}"; + string name = caller.Split(new char[] { '.', '+' }).Last(); + return $"[{dateTimeString}] {name.Substring(0, name.Length >= 13 ? 13 : name.Length),13} | {value}"; + } +} \ No newline at end of file diff --git a/Logging/Logger.cs b/Logging/Logger.cs index a930478..19baa2a 100644 --- a/Logging/Logger.cs +++ b/Logging/Logger.cs @@ -1,5 +1,4 @@ -using System.Net.Mime; -using System.Text; +using System.Text; namespace Logging; @@ -12,24 +11,31 @@ public class Logger : TextWriter ConsoleLogger } - private FileLogger? _fileLogger; - private FormattedConsoleLogger? _formattedConsoleLogger; - private MemoryLogger _memoryLogger; - private TextWriter? stdOut; + private readonly FileLogger? _fileLogger; + private readonly FormattedConsoleLogger? _formattedConsoleLogger; + private readonly MemoryLogger _memoryLogger; public Logger(LoggerType[] enabledLoggers, TextWriter? stdOut, Encoding? encoding, string? logFilePath) { this.Encoding = encoding ?? Encoding.ASCII; - this.stdOut = stdOut ?? null; if (enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is not null) - _fileLogger = new FileLogger(logFilePath, null, encoding); + _fileLogger = new FileLogger(logFilePath, encoding); else { _fileLogger = null; throw new ArgumentException($"logFilePath can not be null for LoggerType {LoggerType.FileLogger}"); } - _formattedConsoleLogger = enabledLoggers.Contains(LoggerType.ConsoleLogger) ? new FormattedConsoleLogger(null, encoding) : null; - _memoryLogger = new MemoryLogger(null, encoding); + + if (enabledLoggers.Contains(LoggerType.ConsoleLogger) && stdOut is not null) + { + _formattedConsoleLogger = new FormattedConsoleLogger(stdOut, encoding); + } + else + { + _formattedConsoleLogger = null; + throw new ArgumentException($"stdOut can not be null for LoggerType {LoggerType.ConsoleLogger}"); + } + _memoryLogger = new MemoryLogger(encoding); } public void WriteLine(string caller, string? value) @@ -46,9 +52,7 @@ public class Logger : TextWriter _fileLogger?.Write(caller, value); _formattedConsoleLogger?.Write(caller, value); - _memoryLogger.Write(caller, value); - stdOut?.Write(value); } public string[] Tail(uint? lines) diff --git a/Logging/LoggerBase.cs b/Logging/LoggerBase.cs index 8e8a72a..0b86074 100644 --- a/Logging/LoggerBase.cs +++ b/Logging/LoggerBase.cs @@ -5,21 +5,10 @@ namespace Logging; public abstract class LoggerBase : TextWriter { public override Encoding Encoding { get; } - protected TextWriter? stdOut { get; } - public LoggerBase(TextWriter? stdOut, Encoding? encoding = null) + public LoggerBase(Encoding? encoding = null) { this.Encoding = encoding ?? Encoding.ASCII; - this.stdOut = stdOut; - } - - public void WriteLine(string caller, string? value) - { - value = value is null ? Environment.NewLine : string.Join(value, Environment.NewLine); - - LogMessage message = new LogMessage(DateTime.Now, caller, value); - - Write(message); } public void Write(string caller, string? value) @@ -27,32 +16,10 @@ public abstract class LoggerBase : TextWriter if (value is null) return; - LogMessage message = new LogMessage(DateTime.Now, caller, value); - - stdOut?.Write(message.ToString()); + LogMessage message = new (DateTime.Now, caller, value); Write(message); } protected abstract void Write(LogMessage message); - - public class LogMessage - { - public DateTime logTime { get; } - public string caller { get; } - public string value { get; } - - public LogMessage(DateTime now, string caller, string value) - { - this.logTime = now; - this.caller = caller; - this.value = value; - } - - public override string ToString() - { - string dateTimeString = $"{logTime.ToShortDateString()} {logTime.ToLongTimeString()}"; - return $"[{dateTimeString}] {caller.Split(new char[]{'.','+'}).Last(),15} | {value}"; - } - } } \ No newline at end of file diff --git a/Logging/MemoryLogger.cs b/Logging/MemoryLogger.cs index 60b8a94..47581f2 100644 --- a/Logging/MemoryLogger.cs +++ b/Logging/MemoryLogger.cs @@ -7,7 +7,7 @@ public class MemoryLogger : LoggerBase private readonly SortedList _logMessages = new(); private int _lastLogMessageIndex = 0; - public MemoryLogger(TextWriter? stdOut, Encoding? encoding = null) : base(stdOut, encoding) + public MemoryLogger(Encoding? encoding = null) : base(encoding) { }