diff --git a/ExtremelySimpleLogger/ConsoleSink.cs b/ExtremelySimpleLogger/ConsoleSink.cs index 35d5e3e..9e2cde2 100644 --- a/ExtremelySimpleLogger/ConsoleSink.cs +++ b/ExtremelySimpleLogger/ConsoleSink.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace ExtremelySimpleLogger { /// @@ -7,15 +8,42 @@ namespace ExtremelySimpleLogger { public class ConsoleSink : Sink { /// - /// The that this console sink should use when printing messages with the log level. + /// The that each is displayed with using this console sink. + /// To edit and query this collection, use and . /// - public ConsoleColor WarnColor { get; set; } = ConsoleColor.DarkYellow; - /// - /// The that this console sink should use when printing messages with the and log levels. - /// - public ConsoleColor ErrorColor { get; set; } = ConsoleColor.DarkRed; + protected readonly Dictionary ConsoleColors = new Dictionary { + {LogLevel.Warn, ConsoleColor.DarkYellow}, + {LogLevel.Error, ConsoleColor.DarkRed}, + {LogLevel.Fatal, ConsoleColor.DarkRed} + }; private readonly object locker = new object(); + /// + /// Sets the that text with the given should be displayed with. + /// To set the default color for the log level, simply pass a with no value to . + /// + /// The log level to set the color for + /// The color to use, or a with no value to clear the current color + public virtual void SetColor(LogLevel level, ConsoleColor? color) { + if (color.HasValue) { + this.ConsoleColors[level] = color.Value; + } else { + this.ConsoleColors.Remove(level); + } + } + + /// + /// Returns the that text with the given is displayed with. + /// If text is displayed with the default console color, a without a value is returned. + /// + /// The log level whose color to query + /// The console color that text with the log level is displayed with, or a with no value if no color is set + public virtual ConsoleColor? GetColor(LogLevel level) { + if (this.ConsoleColors.TryGetValue(level, out var color)) + return color; + return null; + } + /// /// Logs the given message, which has already been formatted using . /// @@ -24,17 +52,12 @@ namespace ExtremelySimpleLogger { /// The message to log protected override void Log(Logger logger, LogLevel level, string s) { lock (this.locker) { - switch (level) { - case LogLevel.Warn: - Console.ForegroundColor = this.WarnColor; - break; - case LogLevel.Error: - case LogLevel.Fatal: - Console.ForegroundColor = this.ErrorColor; - break; - } + var hasColor = this.ConsoleColors.TryGetValue(level, out var color); + if (hasColor) + Console.ForegroundColor = color; Console.WriteLine(s); - Console.ResetColor(); + if (hasColor) + Console.ResetColor(); } }