424 lines
25 KiB
C#
424 lines
25 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Runtime.CompilerServices;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace Swan.Logging {
|
|||
|
/// <summary>
|
|||
|
/// Entry-point for logging. Use this static class to register/unregister
|
|||
|
/// loggers instances. By default, the <c>ConsoleLogger</c> is registered.
|
|||
|
/// </summary>
|
|||
|
public static class Logger {
|
|||
|
private static readonly Object SyncLock = new Object();
|
|||
|
private static readonly List<ILogger> Loggers = new List<ILogger>();
|
|||
|
|
|||
|
private static UInt64 _loggingSequence;
|
|||
|
|
|||
|
static Logger() {
|
|||
|
if(Terminal.IsConsolePresent) {
|
|||
|
Loggers.Add(ConsoleLogger.Instance);
|
|||
|
}
|
|||
|
|
|||
|
if(DebugLogger.IsDebuggerAttached) {
|
|||
|
Loggers.Add(DebugLogger.Instance);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#region Standard Public API
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Registers the logger.
|
|||
|
/// </summary>
|
|||
|
/// <typeparam name="T">The type of logger to register.</typeparam>
|
|||
|
/// <exception cref="InvalidOperationException">There is already a logger with that class registered.</exception>
|
|||
|
public static void RegisterLogger<T>() where T : ILogger {
|
|||
|
lock(SyncLock) {
|
|||
|
ILogger loggerInstance = Loggers.FirstOrDefault(x => x.GetType() == typeof(T));
|
|||
|
|
|||
|
if(loggerInstance != null) {
|
|||
|
throw new InvalidOperationException("There is already a logger with that class registered.");
|
|||
|
}
|
|||
|
|
|||
|
Loggers.Add(Activator.CreateInstance<T>());
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Registers the logger.
|
|||
|
/// </summary>
|
|||
|
/// <param name="logger">The logger.</param>
|
|||
|
public static void RegisterLogger(ILogger logger) {
|
|||
|
lock(SyncLock) {
|
|||
|
Loggers.Add(logger);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Unregisters the logger.
|
|||
|
/// </summary>
|
|||
|
/// <param name="logger">The logger.</param>
|
|||
|
/// <exception cref="ArgumentOutOfRangeException">logger.</exception>
|
|||
|
public static void UnregisterLogger(ILogger logger) => RemoveLogger(x => x == logger);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Unregisters the logger.
|
|||
|
/// </summary>
|
|||
|
/// <typeparam name="T">The type of logger to unregister.</typeparam>
|
|||
|
public static void UnregisterLogger<T>() => RemoveLogger(x => x.GetType() == typeof(T));
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Remove all the loggers.
|
|||
|
/// </summary>
|
|||
|
public static void NoLogging() {
|
|||
|
lock(SyncLock) {
|
|||
|
Loggers.Clear();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#region Debug
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs a debug message to the console.
|
|||
|
/// </summary>
|
|||
|
/// <param name="message">The message.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="extendedData">The extended data.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member. This is automatically populated.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path. This is automatically populated.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number. This is automatically populated.</param>
|
|||
|
public static void Debug(this String message, String source = null, Object extendedData = null, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Debug, message, source, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs a debug message to the console.
|
|||
|
/// </summary>
|
|||
|
/// <param name="message">The message.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="extendedData">The extended data.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number.</param>
|
|||
|
public static void Debug(this String message, Type source, Object extendedData = null, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Debug, message, source?.FullName, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs a debug message to the console.
|
|||
|
/// </summary>
|
|||
|
/// <param name="extendedData">The exception.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="message">The message.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member. This is automatically populated.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path. This is automatically populated.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number. This is automatically populated.</param>
|
|||
|
public static void Debug(this Exception extendedData, String source, String message, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Debug, message, source, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Trace
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs a trace message to the console.
|
|||
|
/// </summary>
|
|||
|
/// <param name="message">The text.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="extendedData">The extended data.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member. This is automatically populated.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path. This is automatically populated.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number. This is automatically populated.</param>
|
|||
|
public static void Trace(this String message, String source = null, Object extendedData = null, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Trace, message, source, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs a trace message to the console.
|
|||
|
/// </summary>
|
|||
|
/// <param name="message">The message.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="extendedData">The extended data.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number.</param>
|
|||
|
public static void Trace(this String message, Type source, Object extendedData = null, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Trace, message, source?.FullName, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs a trace message to the console.
|
|||
|
/// </summary>
|
|||
|
/// <param name="extendedData">The extended data.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="message">The message.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member. This is automatically populated.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path. This is automatically populated.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number. This is automatically populated.</param>
|
|||
|
public static void Trace(this Exception extendedData, String source, String message, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Trace, message, source, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Warn
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs a warning message to the console.
|
|||
|
/// </summary>
|
|||
|
/// <param name="message">The text.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="extendedData">The extended data.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member. This is automatically populated.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path. This is automatically populated.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number. This is automatically populated.</param>
|
|||
|
public static void Warn(this String message, String source = null, Object extendedData = null, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Warning, message, source, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs a warning message to the console.
|
|||
|
/// </summary>
|
|||
|
/// <param name="message">The message.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="extendedData">The extended data.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number.</param>
|
|||
|
public static void Warn(this String message, Type source, Object extendedData = null, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Warning, message, source?.FullName, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs a warning message to the console.
|
|||
|
/// </summary>
|
|||
|
/// <param name="extendedData">The extended data.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="message">The message.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member. This is automatically populated.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path. This is automatically populated.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number. This is automatically populated.</param>
|
|||
|
public static void Warn(this Exception extendedData, String source, String message, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Warning, message, source, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Fatal
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs a warning message to the console.
|
|||
|
/// </summary>
|
|||
|
/// <param name="message">The text.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="extendedData">The extended data.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member. This is automatically populated.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path. This is automatically populated.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number. This is automatically populated.</param>
|
|||
|
public static void Fatal(this String message, String source = null, Object extendedData = null, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Fatal, message, source, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs a warning message to the console.
|
|||
|
/// </summary>
|
|||
|
/// <param name="message">The message.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="extendedData">The extended data.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number.</param>
|
|||
|
public static void Fatal(this String message, Type source, Object extendedData = null, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Fatal, message, source?.FullName, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs a warning message to the console.
|
|||
|
/// </summary>
|
|||
|
/// <param name="extendedData">The extended data.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="message">The message.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member. This is automatically populated.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path. This is automatically populated.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number. This is automatically populated.</param>
|
|||
|
public static void Fatal(this Exception extendedData, String source, String message, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Fatal, message, source, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Info
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs an info message to the console.
|
|||
|
/// </summary>
|
|||
|
/// <param name="message">The text.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="extendedData">The extended data.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member. This is automatically populated.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path. This is automatically populated.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number. This is automatically populated.</param>
|
|||
|
public static void Info(this String message, String source = null, Object extendedData = null, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Info, message, source, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs an info message to the console.
|
|||
|
/// </summary>
|
|||
|
/// <param name="message">The message.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="extendedData">The extended data.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number.</param>
|
|||
|
public static void Info(this String message, Type source, Object extendedData = null, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Info, message, source?.FullName, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs an info message to the console.
|
|||
|
/// </summary>
|
|||
|
/// <param name="extendedData">The extended data.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="message">The message.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member. This is automatically populated.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path. This is automatically populated.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number. This is automatically populated.</param>
|
|||
|
public static void Info(this Exception extendedData, String source, String message, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Info, message, source, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Error
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs an error message to the console's standard error.
|
|||
|
/// </summary>
|
|||
|
/// <param name="message">The text.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="extendedData">The extended data.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member. This is automatically populated.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path. This is automatically populated.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number. This is automatically populated.</param>
|
|||
|
public static void Error(this String message, String source = null, Object extendedData = null, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Error, message, source, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs an error message to the console's standard error.
|
|||
|
/// </summary>
|
|||
|
/// <param name="message">The message.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="extendedData">The extended data.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number.</param>
|
|||
|
public static void Error(this String message, Type source, Object extendedData = null, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Error, message, source?.FullName, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs an error message to the console's standard error.
|
|||
|
/// </summary>
|
|||
|
/// <param name="ex">The exception.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="message">The message.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member. This is automatically populated.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path. This is automatically populated.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number. This is automatically populated.</param>
|
|||
|
public static void Error(this Exception ex, String source, String message, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Error, message, source, ex, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Extended Public API
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs the specified message.
|
|||
|
/// </summary>
|
|||
|
/// <param name="message">The message.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="messageType">Type of the message.</param>
|
|||
|
/// <param name="extendedData">The extended data.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member. This is automatically populated.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path. This is automatically populated.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number. This is automatically populated.</param>
|
|||
|
public static void Log(this String message, String source, LogLevel messageType, Object extendedData = null, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(messageType, message, source, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs the specified message.
|
|||
|
/// </summary>
|
|||
|
/// <param name="message">The message.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="messageType">Type of the message.</param>
|
|||
|
/// <param name="extendedData">The extended data.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number.</param>
|
|||
|
public static void Log(this String message, Type source, LogLevel messageType, Object extendedData = null, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(messageType, message, source?.FullName, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs an error message to the console's standard error.
|
|||
|
/// </summary>
|
|||
|
/// <param name="ex">The ex.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="message">The message.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member. This is automatically populated.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path. This is automatically populated.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number. This is automatically populated.</param>
|
|||
|
public static void Log(this Exception ex, String source = null, String message = null, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Error, message ?? ex.Message, source ?? ex.Source, ex, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs an error message to the console's standard error.
|
|||
|
/// </summary>
|
|||
|
/// <param name="ex">The ex.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="message">The message.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member. This is automatically populated.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path. This is automatically populated.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number. This is automatically populated.</param>
|
|||
|
public static void Log(this Exception ex, Type source = null, String message = null, [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) => LogMessage(LogLevel.Error, message ?? ex.Message, source?.FullName ?? ex.Source, ex, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs a trace message showing all possible non-null properties of the given object
|
|||
|
/// This method is expensive as it uses Stringify internally.
|
|||
|
/// </summary>
|
|||
|
/// <param name="obj">The object.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="text">The title.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member. This is automatically populated.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path. This is automatically populated.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number. This is automatically populated.</param>
|
|||
|
public static void Dump(this Object obj, String source, String text = "Object Dump", [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) {
|
|||
|
if(obj == null) {
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
String message = $"{text} ({obj.GetType()}): {Environment.NewLine}{obj.Stringify().Indent(5)}";
|
|||
|
LogMessage(LogLevel.Trace, message, source, obj, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Logs a trace message showing all possible non-null properties of the given object
|
|||
|
/// This method is expensive as it uses Stringify internally.
|
|||
|
/// </summary>
|
|||
|
/// <param name="obj">The object.</param>
|
|||
|
/// <param name="source">The source.</param>
|
|||
|
/// <param name="text">The text.</param>
|
|||
|
/// <param name="callerMemberName">Name of the caller member.</param>
|
|||
|
/// <param name="callerFilePath">The caller file path.</param>
|
|||
|
/// <param name="callerLineNumber">The caller line number.</param>
|
|||
|
public static void Dump(this Object obj, Type source, String text = "Object Dump", [CallerMemberName] String callerMemberName = "", [CallerFilePath] String callerFilePath = "", [CallerLineNumber] Int32 callerLineNumber = 0) {
|
|||
|
if(obj == null) {
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
String message = $"{text} ({obj.GetType()}): {Environment.NewLine}{obj.Stringify().Indent(5)}";
|
|||
|
LogMessage(LogLevel.Trace, message, source?.FullName, obj, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
private static void RemoveLogger(Func<ILogger, Boolean> criteria) {
|
|||
|
lock(SyncLock) {
|
|||
|
ILogger loggerInstance = Loggers.FirstOrDefault(criteria);
|
|||
|
|
|||
|
if(loggerInstance == null) {
|
|||
|
throw new InvalidOperationException("The logger is not registered.");
|
|||
|
}
|
|||
|
|
|||
|
loggerInstance.Dispose();
|
|||
|
|
|||
|
_ = Loggers.Remove(loggerInstance);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private static void LogMessage(LogLevel logLevel, String message, String sourceName, Object extendedData, String callerMemberName, String callerFilePath, Int32 callerLineNumber) {
|
|||
|
UInt64 sequence = _loggingSequence;
|
|||
|
DateTime date = DateTime.UtcNow;
|
|||
|
_loggingSequence++;
|
|||
|
|
|||
|
String loggerMessage = String.IsNullOrWhiteSpace(message) ? String.Empty : message.RemoveControlCharsExcept('\n');
|
|||
|
|
|||
|
LogMessageReceivedEventArgs eventArgs = new LogMessageReceivedEventArgs(sequence, logLevel, date, sourceName, loggerMessage, extendedData, callerMemberName, callerFilePath, callerLineNumber);
|
|||
|
|
|||
|
foreach(ILogger logger in Loggers) {
|
|||
|
_ = Task.Run(() => {
|
|||
|
if(logger.LogLevel <= logLevel) {
|
|||
|
logger.Log(eventArgs);
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|