using System;
using System.Diagnostics;
namespace WPFMediaKit;
///
/// A logger.
///
public interface ILog
{
///
/// If the info level is enabled.
///
bool IsInfoEnabled { get; }
///
/// If the debug level is enabled.
///
bool IsDebugEnabled { get; }
///
/// Logs the error message with a possible exception.
///
/// The exception. May be null.
/// A (formatted) message.
/// Arguments to the formatted message. May be missing.
void Error(Exception exception, string message, params object[] args);
///
/// Logs the warning message.
///
/// A (formatted) message.
/// Arguments to the formatted message. May be missing.
void Warn(string message, params object[] args);
///
/// Logs the info message.
///
/// A (formatted) message.
/// Arguments to the formatted message. May be missing.
void Info(string message, params object[] args);
///
/// Logs the debug message.
///
/// A (formatted) message.
/// Arguments to the formatted message. May be missing.
void Debug(string message, params object[] args);
}
public static class ILogExtensions
{
///
/// Log the error without an exception.
///
public static void Error(this ILog thiz, string message, params object[] args)
=> thiz.Error(null, message, args);
}
///
/// Logging to .
///
public class DebugTraceLog : ILog
{
private string _name;
public DebugTraceLog(string name)
{
_name = name;
}
public virtual bool IsInfoEnabled => true;
public virtual bool IsDebugEnabled => true;
public virtual void Error(Exception exception, string message, params object[] args)
{
Trace.TraceError(AddName(message), args);
if (exception != null)
Trace.TraceError(exception.ToString());
}
public virtual void Warn(string message, params object[] args)
{
Trace.TraceWarning(AddName(message), args);
}
public virtual void Info(string message, params object[] args)
{
if (!IsInfoEnabled)
return;
Trace.TraceInformation(AddName(message), args);
}
public virtual void Debug(string message, params object[] args)
{
if (!IsDebugEnabled)
return;
Trace.WriteLine(string.Format(AddName(message), args));
}
protected virtual string AddName(string message)
{
if (string.IsNullOrEmpty(_name))
return message;
return _name + " - " + message;
}
}
///
/// No-op implementation of ILog.
///
public class NullLog : ILog
{
private static NullLog _instance;
public static NullLog Instance
=> _instance != null ? _instance : (_instance = new NullLog());
public bool IsInfoEnabled => false;
public virtual bool IsDebugEnabled => false;
public void Error(Exception exception, string message, params object[] args)
{ }
public void Info(string message, params object[] args)
{ }
public void Debug(string message, params object[] args)
{ }
public void Warn(string message, params object[] args)
{ }
}