improved GetColor and SetColor

This commit is contained in:
Ell 2021-07-14 23:29:13 +02:00
parent 55bb8ff4cf
commit b5c0600560

View file

@ -24,12 +24,14 @@ namespace ExtremelySimpleLogger {
/// </summary> /// </summary>
/// <param name="level">The log level to set the color for</param> /// <param name="level">The log level to set the color for</param>
/// <param name="color">The color to use, or a <see cref="Nullable{ConsoleColor}"/> with no value to clear the current color</param> /// <param name="color">The color to use, or a <see cref="Nullable{ConsoleColor}"/> with no value to clear the current color</param>
public virtual void SetColor(LogLevel level, ConsoleColor? color) { /// <returns>This instance, for chaining</returns>
public virtual ConsoleSink SetColor(LogLevel level, ConsoleColor? color) {
if (color.HasValue) { if (color.HasValue) {
this.ConsoleColors[level] = color.Value; this.ConsoleColors[level] = color.Value;
} else { } else {
this.ConsoleColors.Remove(level); this.ConsoleColors.Remove(level);
} }
return this;
} }
/// <summary> /// <summary>
@ -52,11 +54,11 @@ namespace ExtremelySimpleLogger {
/// <param name="s">The message to log</param> /// <param name="s">The message to log</param>
protected override void Log(Logger logger, LogLevel level, string s) { protected override void Log(Logger logger, LogLevel level, string s) {
lock (this.locker) { lock (this.locker) {
var hasColor = this.ConsoleColors.TryGetValue(level, out var color); var color = this.GetColor(level);
if (hasColor) if (color.HasValue)
Console.ForegroundColor = color; Console.ForegroundColor = color.Value;
Console.WriteLine(s); Console.WriteLine(s);
if (hasColor) if (color.HasValue)
Console.ResetColor(); Console.ResetColor();
} }
} }