RaspberryIO_26/Swan.Lite/Logging/ConsoleLogger.cs

140 lines
3.9 KiB
C#
Raw Normal View History

2019-12-04 18:57:18 +01:00
using System;
using Swan.Lite.Logging;
2019-12-08 19:54:52 +01:00
namespace Swan.Logging {
/// <summary>
/// Represents a Console implementation of <c>ILogger</c>.
/// </summary>
/// <seealso cref="ILogger" />
public class ConsoleLogger : TextLogger, ILogger {
2019-12-04 18:57:18 +01:00
/// <summary>
2019-12-08 19:54:52 +01:00
/// Initializes a new instance of the <see cref="ConsoleLogger"/> class.
2019-12-04 18:57:18 +01:00
/// </summary>
2019-12-08 19:54:52 +01:00
protected ConsoleLogger() {
// Empty
}
/// <summary>
/// Gets the current instance of ConsoleLogger.
/// </summary>
/// <value>
/// The instance.
/// </value>
public static ConsoleLogger Instance { get; } = new ConsoleLogger();
/// <summary>
/// Gets or sets the debug logging prefix.
/// </summary>
/// <value>
/// The debug prefix.
/// </value>
public static String DebugPrefix { get; set; } = "DBG";
/// <summary>
/// Gets or sets the trace logging prefix.
/// </summary>
/// <value>
/// The trace prefix.
/// </value>
public static String TracePrefix { get; set; } = "TRC";
/// <summary>
/// Gets or sets the warning logging prefix.
/// </summary>
/// <value>
/// The warn prefix.
/// </value>
public static String WarnPrefix { get; set; } = "WRN";
/// <summary>
/// Gets or sets the fatal logging prefix.
/// </summary>
/// <value>
/// The fatal prefix.
/// </value>
public static String FatalPrefix { get; set; } = "FAT";
/// <summary>
/// Gets or sets the error logging prefix.
/// </summary>
/// <value>
/// The error prefix.
/// </value>
public static String ErrorPrefix { get; set; } = "ERR";
/// <summary>
/// Gets or sets the information logging prefix.
/// </summary>
/// <value>
/// The information prefix.
/// </value>
public static String InfoPrefix { get; set; } = "INF";
/// <summary>
/// Gets or sets the color of the information output logging.
/// </summary>
/// <value>
/// The color of the information.
/// </value>
public static ConsoleColor InfoColor { get; set; } = ConsoleColor.Cyan;
/// <summary>
/// Gets or sets the color of the debug output logging.
/// </summary>
/// <value>
/// The color of the debug.
/// </value>
public static ConsoleColor DebugColor { get; set; } = ConsoleColor.Gray;
/// <summary>
/// Gets or sets the color of the trace output logging.
/// </summary>
/// <value>
/// The color of the trace.
/// </value>
public static ConsoleColor TraceColor { get; set; } = ConsoleColor.DarkGray;
/// <summary>
/// Gets or sets the color of the warning logging.
/// </summary>
/// <value>
/// The color of the warn.
/// </value>
public static ConsoleColor WarnColor { get; set; } = ConsoleColor.Yellow;
/// <summary>
/// Gets or sets the color of the error logging.
/// </summary>
/// <value>
/// The color of the error.
/// </value>
public static ConsoleColor ErrorColor { get; set; } = ConsoleColor.DarkRed;
/// <summary>
/// Gets or sets the color of the error logging.
/// </summary>
/// <value>
/// The color of the error.
/// </value>
public static ConsoleColor FatalColor { get; set; } = ConsoleColor.Red;
/// <inheritdoc />
public LogLevel LogLevel { get; set; } = DebugLogger.IsDebuggerAttached ? LogLevel.Trace : LogLevel.Info;
/// <inheritdoc />
public void Log(LogMessageReceivedEventArgs logEvent) {
// Select the writer based on the message type
TerminalWriters writer = logEvent.MessageType == LogLevel.Error ? TerminalWriters.StandardError : TerminalWriters.StandardOutput;
(String outputMessage, ConsoleColor color) = this.GetOutputAndColor(logEvent);
Terminal.Write(outputMessage, color, writer);
}
/// <inheritdoc />
public void Dispose() {
// Do nothing
}
}
2019-12-04 18:57:18 +01:00
}