mirror of
https://github.com/Ellpeck/ExtremelySimpleLogger.git
synced 2024-11-22 01:53:29 +01:00
added proper ways to get and set the console color of ConsoleSink
This commit is contained in:
parent
67958a937f
commit
f6db44c507
1 changed files with 39 additions and 16 deletions
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ExtremelySimpleLogger {
|
||||
/// <summary>
|
||||
|
@ -7,15 +8,42 @@ namespace ExtremelySimpleLogger {
|
|||
public class ConsoleSink : Sink {
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ConsoleColor"/> that this console sink should use when printing messages with the <see cref="LogLevel.Warn"/> log level.
|
||||
/// The <see cref="ConsoleColors"/> that each <see cref="LogLevel"/> is displayed with using this console sink.
|
||||
/// To edit and query this collection, use <see cref="SetColor"/> and <see cref="GetColor"/>.
|
||||
/// </summary>
|
||||
public ConsoleColor WarnColor { get; set; } = ConsoleColor.DarkYellow;
|
||||
/// <summary>
|
||||
/// The <see cref="ConsoleColor"/> that this console sink should use when printing messages with the <see cref="LogLevel.Error"/> and <see cref="LogLevel.Fatal"/> log levels.
|
||||
/// </summary>
|
||||
public ConsoleColor ErrorColor { get; set; } = ConsoleColor.DarkRed;
|
||||
protected readonly Dictionary<LogLevel, ConsoleColor> ConsoleColors = new Dictionary<LogLevel, ConsoleColor> {
|
||||
{LogLevel.Warn, ConsoleColor.DarkYellow},
|
||||
{LogLevel.Error, ConsoleColor.DarkRed},
|
||||
{LogLevel.Fatal, ConsoleColor.DarkRed}
|
||||
};
|
||||
private readonly object locker = new object();
|
||||
|
||||
/// <summary>
|
||||
/// Sets the <see cref="ConsoleColor"/> that text with the given <see cref="LogLevel"/> should be displayed with.
|
||||
/// To set the default color for the log level, simply pass a <see cref="Nullable{ConsoleColor}"/> with no value to <paramref name="color"/>.
|
||||
/// </summary>
|
||||
/// <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>
|
||||
public virtual void SetColor(LogLevel level, ConsoleColor? color) {
|
||||
if (color.HasValue) {
|
||||
this.ConsoleColors[level] = color.Value;
|
||||
} else {
|
||||
this.ConsoleColors.Remove(level);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the <see cref="ConsoleColor"/> that text with the given <see cref="LogLevel"/> is displayed with.
|
||||
/// If text is displayed with the default console color, a <see cref="Nullable{ConsoleColor}"/> without a value is returned.
|
||||
/// </summary>
|
||||
/// <param name="level">The log level whose color to query</param>
|
||||
/// <returns>The console color that text with the log level is displayed with, or a <see cref="Nullable{ConsoleColor}"/> with no value if no color is set</returns>
|
||||
public virtual ConsoleColor? GetColor(LogLevel level) {
|
||||
if (this.ConsoleColors.TryGetValue(level, out var color))
|
||||
return color;
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/>.
|
||||
/// </summary>
|
||||
|
@ -24,16 +52,11 @@ namespace ExtremelySimpleLogger {
|
|||
/// <param name="s">The message to log</param>
|
||||
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);
|
||||
if (hasColor)
|
||||
Console.ResetColor();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue