2
0

Better Logger.

Includes a formatted Console-Log
This commit is contained in:
glax 2023-07-16 17:33:15 +02:00
parent 0f8932e712
commit a897a7b3a2
6 changed files with 50 additions and 57 deletions

View File

@ -1,5 +1,4 @@
using System.Text; using System.Text;
using System.Text.Json.Serialization;
namespace Logging; namespace Logging;
@ -8,7 +7,7 @@ public class FileLogger : LoggerBase
private string logFilePath { get; } private string logFilePath { get; }
private const int MaxNumberOfLogFiles = 5; 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; this.logFilePath = logFilePath;
@ -22,11 +21,11 @@ public class FileLogger : LoggerBase
{ {
try try
{ {
File.AppendAllText(logFilePath, logMessage.ToString()); File.AppendAllText(logFilePath, logMessage.formattedMessage);
} }
catch (Exception e) catch (Exception)
{ {
stdOut?.WriteLine(e); // ignored
} }
} }
} }

View File

@ -4,14 +4,14 @@ namespace Logging;
public class FormattedConsoleLogger : LoggerBase public class FormattedConsoleLogger : LoggerBase
{ {
private readonly TextWriter _stdOut;
public FormattedConsoleLogger(TextWriter? stdOut, Encoding? encoding = null) : base(stdOut, encoding) public FormattedConsoleLogger(TextWriter stdOut, Encoding? encoding = null) : base(encoding)
{ {
this._stdOut = stdOut;
} }
protected override void Write(LogMessage message) protected override void Write(LogMessage message)
{ {
//Nothing to do yet this._stdOut.Write(message.formattedMessage);
} }
} }

23
Logging/LogMessage.cs Normal file
View File

@ -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}";
}
}

View File

@ -1,5 +1,4 @@
using System.Net.Mime; using System.Text;
using System.Text;
namespace Logging; namespace Logging;
@ -12,24 +11,31 @@ public class Logger : TextWriter
ConsoleLogger ConsoleLogger
} }
private FileLogger? _fileLogger; private readonly FileLogger? _fileLogger;
private FormattedConsoleLogger? _formattedConsoleLogger; private readonly FormattedConsoleLogger? _formattedConsoleLogger;
private MemoryLogger _memoryLogger; private readonly MemoryLogger _memoryLogger;
private TextWriter? stdOut;
public Logger(LoggerType[] enabledLoggers, TextWriter? stdOut, Encoding? encoding, string? logFilePath) public Logger(LoggerType[] enabledLoggers, TextWriter? stdOut, Encoding? encoding, string? logFilePath)
{ {
this.Encoding = encoding ?? Encoding.ASCII; this.Encoding = encoding ?? Encoding.ASCII;
this.stdOut = stdOut ?? null;
if (enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is not null) if (enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is not null)
_fileLogger = new FileLogger(logFilePath, null, encoding); _fileLogger = new FileLogger(logFilePath, encoding);
else else
{ {
_fileLogger = null; _fileLogger = null;
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;
_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) public void WriteLine(string caller, string? value)
@ -46,9 +52,7 @@ public class Logger : TextWriter
_fileLogger?.Write(caller, value); _fileLogger?.Write(caller, value);
_formattedConsoleLogger?.Write(caller, value); _formattedConsoleLogger?.Write(caller, value);
_memoryLogger.Write(caller, value); _memoryLogger.Write(caller, value);
stdOut?.Write(value);
} }
public string[] Tail(uint? lines) public string[] Tail(uint? lines)

View File

@ -5,21 +5,10 @@ namespace Logging;
public abstract class LoggerBase : TextWriter public abstract class LoggerBase : TextWriter
{ {
public override Encoding Encoding { get; } 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.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) public void Write(string caller, string? value)
@ -27,32 +16,10 @@ public abstract class LoggerBase : TextWriter
if (value is null) if (value is null)
return; return;
LogMessage message = new LogMessage(DateTime.Now, caller, value); LogMessage message = new (DateTime.Now, caller, value);
stdOut?.Write(message.ToString());
Write(message); Write(message);
} }
protected abstract void Write(LogMessage 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}";
}
}
} }

View File

@ -7,7 +7,7 @@ public class MemoryLogger : LoggerBase
private readonly SortedList<DateTime, LogMessage> _logMessages = new(); private readonly SortedList<DateTime, LogMessage> _logMessages = new();
private int _lastLogMessageIndex = 0; private int _lastLogMessageIndex = 0;
public MemoryLogger(TextWriter? stdOut, Encoding? encoding = null) : base(stdOut, encoding) public MemoryLogger(Encoding? encoding = null) : base(encoding)
{ {
} }