Last active
November 23, 2022 21:10
-
-
Save JuergenGutsch/b6e7787ed5c3e0a5fd4681497a2c1f4c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Concurrent; | |
namespace LoggingSample; | |
public class ColoredConsoleLoggerConfiguration | |
{ | |
public LogLevel LogLevel { get; set; } = LogLevel.Warning; | |
public int EventId { get; set; } = 0; | |
public ConsoleColor Color { get; set; } = ConsoleColor.Yellow; | |
} | |
public class ColoredConsoleLoggerProvider : ILoggerProvider | |
{ | |
private readonly ColoredConsoleLoggerConfiguration _config; | |
private readonly ConcurrentDictionary<string, ColoredConsoleLogger> _loggers = | |
new ConcurrentDictionary<string, ColoredConsoleLogger>(); | |
public ColoredConsoleLoggerProvider(ColoredConsoleLoggerConfiguration config) | |
{ | |
_config = config; | |
} | |
public ILogger CreateLogger(string categoryName) | |
{ | |
return _loggers.GetOrAdd(categoryName, name => | |
new ColoredConsoleLogger(name, _config)); | |
} | |
public void Dispose() | |
{ | |
_loggers.Clear(); | |
} | |
} | |
public class ColoredConsoleLogger : ILogger | |
{ | |
private static readonly object _lock = new(); | |
private readonly string _name; | |
private readonly ColoredConsoleLoggerConfiguration _config; | |
public ColoredConsoleLogger( | |
string name, | |
ColoredConsoleLoggerConfiguration config) | |
{ | |
_name = name; | |
_config = config; | |
} | |
public IDisposable BeginScope<TState>(TState state) | |
{ | |
return null; | |
} | |
public bool IsEnabled(LogLevel logLevel) | |
{ | |
return logLevel == _config.LogLevel; | |
} | |
public void Log<TState>( | |
LogLevel logLevel, | |
EventId eventId, | |
TState state, | |
Exception? exception, | |
Func<TState, Exception?, string> formatter) | |
{ | |
if (!IsEnabled(logLevel)) | |
{ | |
return; | |
} | |
lock (_lock) | |
{ | |
if (_config.EventId == 0 || _config.EventId == eventId.Id) | |
{ | |
var color = Console.ForegroundColor; | |
Console.ForegroundColor = _config.Color; | |
Console.Write($"{logLevel} - "); | |
Console.Write($"{eventId.Id} - {_name} - "); | |
Console.Write($"{formatter(state, exception)}\n"); | |
Console.ForegroundColor = color; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment