ExtremelySimpleLogger/ExtremelySimpleLogger/ConsoleSink.cs

42 lines
1.8 KiB
C#
Raw Normal View History

2020-07-23 02:24:18 +02:00
using System;
namespace ExtremelySimpleLogger {
2020-07-23 02:46:34 +02:00
/// <summary>
/// A <see cref="Sink"/> that writes log output to <see cref="Console.Out"/>.
/// </summary>
2020-07-23 02:24:18 +02:00
public class ConsoleSink : Sink {
2020-07-29 14:23:23 +02:00
/// <summary>
/// The <see cref="ConsoleColor"/> that this console sink should use when printing messages with the <see cref="LogLevel.Warn"/> log level.
/// </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;
private readonly object locker = new object();
2020-07-23 02:46:34 +02:00
/// <summary>
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/>.
/// </summary>
2020-07-29 14:23:23 +02:00
/// <param name="logger">The logger that the message was passed to</param>
/// <param name="level">The importance level of this message</param>
2020-07-23 02:46:34 +02:00
/// <param name="s">The message to log</param>
2020-07-29 14:23:23 +02:00
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;
}
Console.WriteLine(s);
Console.ResetColor();
}
2020-07-23 02:24:18 +02:00
}
}
}