diff --git a/GlaxLogger/GlaxLogger.csproj b/GlaxLogger/GlaxLogger.csproj
index 57ce128..12509a3 100644
--- a/GlaxLogger/GlaxLogger.csproj
+++ b/GlaxLogger/GlaxLogger.csproj
@@ -7,7 +7,7 @@
Glax
https://git.bernloehr.eu/glax/GlaxLogger
git
- 1.0.2
+ 1.0.3
diff --git a/GlaxLogger/Logger.cs b/GlaxLogger/Logger.cs
index fcb9ba2..f879ca4 100644
--- a/GlaxLogger/Logger.cs
+++ b/GlaxLogger/Logger.cs
@@ -3,20 +3,22 @@ using Microsoft.Extensions.Logging;
namespace GlaxLogger;
-public class Logger : ILogger
+public class Logger : ILogger, IDisposable, IAsyncDisposable
{
private readonly LogLevel _filterLevel;
private readonly FileStream _allMessageLogfile, _filteredLogfile;
private readonly ConsoleColor _defaultForegroundColor = Console.ForegroundColor;
private readonly ConsoleColor _defaultBackgroundColor = Console.BackgroundColor;
+ private readonly TextWriter _consoleOut;
- public Logger(LogLevel filteredLevel = LogLevel.Warning, string? outputFolderPath = null)
+ public Logger(LogLevel filteredLevel = LogLevel.Warning, string? outputFolderPath = null, TextWriter? consoleOut = null)
{
this._filterLevel = filteredLevel;
string logFolderPath = outputFolderPath ?? Environment.CurrentDirectory;
- this._filteredLogfile = new FileStream(Path.Join(logFolderPath, $"{DateTime.Now:yyyy-MM-dd hh.mm.ss}-filtered.log"), FileMode.Create, FileAccess.Write, FileShare.Read);
- this._allMessageLogfile = new FileStream(Path.Join(logFolderPath, $"{DateTime.Now:yyyy-MM-dd hh.mm.ss}.log"), FileMode.Create, FileAccess.Write, FileShare.Read);
+ this._filteredLogfile = new FileStream(Path.Join(logFolderPath, $"{DateTime.Now:yyyy-MM-dd HH.mm.ss}-filtered.log"), FileMode.Create);
+ this._allMessageLogfile = new FileStream(Path.Join(logFolderPath, $"{DateTime.Now:yyyy-MM-dd HH.mm.ss}.log"), FileMode.Create);
+ this._consoleOut = consoleOut ?? Console.Out;
}
public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter)
@@ -24,19 +26,19 @@ public class Logger : ILogger
string logLevelStr = logLevel.ToString()[..3].ToUpper();
string timeStr = $"[{DateTime.UtcNow:HH:mm:ss.fff}]";
string messageStr = formatter.Invoke(state, exception);
- string fullMessage = $"{logLevelStr} [{timeStr}] {messageStr}";
+ string fullMessage = $"{logLevelStr} {timeStr} {messageStr.Replace("\n","\n\t")}\n\r";
byte[] fullMessageBytes = Encoding.UTF8.GetBytes(fullMessage);
- _allMessageLogfile.Write(fullMessageBytes, 0, fullMessageBytes.Length);
+ _allMessageLogfile.Write(fullMessageBytes);
if (!IsEnabled(logLevel))
return;
- _filteredLogfile.Write(fullMessageBytes, 0, fullMessageBytes.Length);
+ _filteredLogfile.Write(fullMessageBytes);
Console.ForegroundColor = ForegroundColorForLogLevel(logLevel);
Console.BackgroundColor = BackgroundColorForLogLevel(logLevel);
- Console.Write(logLevelStr);
+ _consoleOut.Write(logLevelStr);
Console.ResetColor();
// ReSharper disable once LocalizableElement
- Console.Write($" {timeStr} ");
- Console.WriteLine(messageStr);
+ _consoleOut.Write($" {timeStr} ");
+ _consoleOut.WriteLine(messageStr);
}
public bool IsEnabled(LogLevel logLevel)
@@ -71,4 +73,17 @@ public class Logger : ILogger
};
}
+ public void Dispose()
+ {
+ _allMessageLogfile.Dispose();
+ _filteredLogfile.Dispose();
+ _consoleOut.Dispose();
+ }
+
+ public async ValueTask DisposeAsync()
+ {
+ await _allMessageLogfile.DisposeAsync();
+ await _filteredLogfile.DisposeAsync();
+ await _consoleOut.DisposeAsync();
+ }
}
\ No newline at end of file