mirror of
https://github.com/Ellpeck/ExtremelySimpleLogger.git
synced 2024-11-22 10:03: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;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace ExtremelySimpleLogger {
|
namespace ExtremelySimpleLogger {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -7,15 +8,42 @@ namespace ExtremelySimpleLogger {
|
||||||
public class ConsoleSink : Sink {
|
public class ConsoleSink : Sink {
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
public ConsoleColor WarnColor { get; set; } = ConsoleColor.DarkYellow;
|
protected readonly Dictionary<LogLevel, ConsoleColor> ConsoleColors = new Dictionary<LogLevel, ConsoleColor> {
|
||||||
/// <summary>
|
{LogLevel.Warn, ConsoleColor.DarkYellow},
|
||||||
/// 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.
|
{LogLevel.Error, ConsoleColor.DarkRed},
|
||||||
/// </summary>
|
{LogLevel.Fatal, ConsoleColor.DarkRed}
|
||||||
public ConsoleColor ErrorColor { get; set; } = ConsoleColor.DarkRed;
|
};
|
||||||
private readonly object locker = new object();
|
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>
|
/// <summary>
|
||||||
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/>.
|
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -24,17 +52,12 @@ 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) {
|
||||||
switch (level) {
|
var hasColor = this.ConsoleColors.TryGetValue(level, out var color);
|
||||||
case LogLevel.Warn:
|
if (hasColor)
|
||||||
Console.ForegroundColor = this.WarnColor;
|
Console.ForegroundColor = color;
|
||||||
break;
|
|
||||||
case LogLevel.Error:
|
|
||||||
case LogLevel.Fatal:
|
|
||||||
Console.ForegroundColor = this.ErrorColor;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
Console.WriteLine(s);
|
Console.WriteLine(s);
|
||||||
Console.ResetColor();
|
if (hasColor)
|
||||||
|
Console.ResetColor();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue