Message Formatting, Console output
This commit is contained in:
parent
87607165c3
commit
13ba0fedea
@ -7,7 +7,7 @@
|
||||
<Authors>Glax</Authors>
|
||||
<RepositoryUrl>https://git.bernloehr.eu/glax/GlaxLogger</RepositoryUrl>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<Version>1.0.2</Version>
|
||||
<Version>1.0.3</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -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<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user