|
| 1 | +using Microsoft.Extensions.Logging; |
| 2 | +using Microsoft.Extensions.Logging.Console; |
| 3 | +using Microsoft.Extensions.Logging.Abstractions; |
| 4 | +using static Crayon.Output; |
| 5 | + |
| 6 | +namespace systray_doom; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// A custom console formatter that uses Crayon for coloring and outputs logs in a single line. |
| 10 | +/// </summary> |
| 11 | +public sealed class CrayonConsoleFormatter : ConsoleFormatter |
| 12 | +{ |
| 13 | + public CrayonConsoleFormatter() : base(nameof(CrayonConsoleFormatter)) |
| 14 | + { |
| 15 | + } |
| 16 | + |
| 17 | + public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeProvider? scopeProvider, TextWriter textWriter) |
| 18 | + { |
| 19 | + var message = logEntry.Formatter(logEntry.State, logEntry.Exception); |
| 20 | + if (string.IsNullOrEmpty(message)) |
| 21 | + { |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + var logLevel = logEntry.LogLevel; |
| 26 | + var categoryName = logEntry.Category; |
| 27 | + var timestamp = DateTime.Now.ToString("HH:mm:ss.fff"); |
| 28 | + |
| 29 | + // Apply colors based on log level |
| 30 | + var levelText = GetLogLevelText(logLevel); |
| 31 | + var coloredLevel = ApplyLogLevelColor(levelText, logLevel); |
| 32 | + var coloredMessage = ApplyMessageColor(message, logLevel); |
| 33 | + |
| 34 | + // Format: [HH:mm:ss.fff] LEVEL Category: Message |
| 35 | + textWriter.WriteLine($"{Dim($"[{timestamp}]")} {coloredLevel} {Dim($"{categoryName}:")} {coloredMessage}"); |
| 36 | + |
| 37 | + // Write exception if present |
| 38 | + if (logEntry.Exception != null) |
| 39 | + { |
| 40 | + textWriter.WriteLine(Red(logEntry.Exception.ToString())); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private static string GetLogLevelText(LogLevel logLevel) => logLevel switch |
| 45 | + { |
| 46 | + LogLevel.Trace => "TRCE", |
| 47 | + LogLevel.Debug => "DBUG", |
| 48 | + LogLevel.Information => "INFO", |
| 49 | + LogLevel.Warning => "WARN", |
| 50 | + LogLevel.Error => "ERRO", |
| 51 | + LogLevel.Critical => "CRIT", |
| 52 | + LogLevel.None => "NONE", |
| 53 | + _ => "UNKN" |
| 54 | + }; |
| 55 | + |
| 56 | + private static string ApplyLogLevelColor(string levelText, LogLevel logLevel) => logLevel switch |
| 57 | + { |
| 58 | + LogLevel.Trace => Dim(levelText), |
| 59 | + LogLevel.Debug => Dim(levelText), |
| 60 | + LogLevel.Information => levelText, |
| 61 | + LogLevel.Warning => Yellow(levelText), |
| 62 | + LogLevel.Error => Red(levelText), |
| 63 | + LogLevel.Critical => Bold(Red(levelText)), |
| 64 | + _ => levelText |
| 65 | + }; |
| 66 | + |
| 67 | + private static string ApplyMessageColor(string message, LogLevel logLevel) => logLevel switch |
| 68 | + { |
| 69 | + LogLevel.Trace => Dim(message), |
| 70 | + LogLevel.Debug => Dim(message), |
| 71 | + LogLevel.Information => message, |
| 72 | + LogLevel.Warning => Yellow(message), |
| 73 | + LogLevel.Error => Red(message), |
| 74 | + LogLevel.Critical => Bold(Red(message)), |
| 75 | + _ => message |
| 76 | + }; |
| 77 | +} |
0 commit comments