124 lines
2.5 KiB
C#
124 lines
2.5 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
|
|
namespace MAX.Core.Logger;
|
|
|
|
/// <summary>
|
|
/// Implementation of <see cref="ILoggerBase"/> that logs into a <see cref="TextWriter"/>.
|
|
/// </summary>
|
|
public class ConsoleLogger : ILoggerFacade
|
|
{
|
|
private readonly bool _debug;
|
|
private readonly bool _console;
|
|
|
|
public bool IsDebugEnabled => true;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of <see cref="ConsoleLogger"/> that writes to
|
|
/// the console output.
|
|
/// </summary>
|
|
public ConsoleLogger(bool debug = true, bool console = true)
|
|
{
|
|
_debug = debug;
|
|
_console = console;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///// <summary>
|
|
///// Disposes the associated <see cref="TextWriter"/>.
|
|
///// </summary>
|
|
///// <param name="disposing">When <see langword="true"/>, disposes the associated <see cref="TextWriter"/>.</param>
|
|
//protected virtual void Dispose(bool disposing)
|
|
//{
|
|
// if (disposing)
|
|
// {
|
|
// writer?.Dispose();
|
|
// }
|
|
//}
|
|
|
|
///<summary>
|
|
///Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
///</summary>
|
|
/// <remarks>Calls <see cref="Dispose(bool)"/></remarks>.
|
|
///<filterpriority>2</filterpriority>
|
|
public void Dispose()
|
|
{
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
public void Debug(Exception ex, string message)
|
|
{
|
|
Log(message, LogLevel.Debug);
|
|
}
|
|
|
|
public void Debug(string message)
|
|
{
|
|
Log(message, LogLevel.Debug);
|
|
}
|
|
|
|
public void Info(Exception ex, string message)
|
|
{
|
|
Log(message, LogLevel.Info);
|
|
}
|
|
|
|
public void Info(string message)
|
|
{
|
|
Log(message, LogLevel.Info);
|
|
}
|
|
|
|
public void Warn(Exception ex, string message)
|
|
{
|
|
Log(message, LogLevel.Warn);
|
|
}
|
|
|
|
public void Warn(string message)
|
|
{
|
|
Log(message, LogLevel.Warn);
|
|
}
|
|
|
|
public void Error(Exception ex, string message)
|
|
{
|
|
Log(message, LogLevel.Error);
|
|
}
|
|
|
|
public void Error(string message)
|
|
{
|
|
Log(message, LogLevel.Error);
|
|
}
|
|
|
|
public void Fatal(Exception ex, string message)
|
|
{
|
|
Log(message, LogLevel.Fatal);
|
|
}
|
|
|
|
public void Fatal(string message)
|
|
{
|
|
Log(message, LogLevel.Fatal);
|
|
}
|
|
|
|
public void Error(Exception ex)
|
|
{
|
|
Log(ex.Message, LogLevel.Error);
|
|
}
|
|
|
|
public void Log(string message, LogLevel level)
|
|
{
|
|
string messageToLog = string.Format(CultureInfo.InvariantCulture, "{1}: {2}. Timestamp:{0:u}.", DateTime.Now,
|
|
level.ToString().ToUpper(CultureInfo.InvariantCulture), message);
|
|
//Trace.WriteLine(messageToLog);
|
|
if (_debug)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine(messageToLog);
|
|
}
|
|
if (_console)
|
|
{
|
|
Console.WriteLine(messageToLog);
|
|
}
|
|
}
|
|
} |