250 lines
7.9 KiB
C#
250 lines
7.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using JD.Kernel.StaticHelpers;
|
|
using JD.LogService.Loggers;
|
|
|
|
namespace JD.LogService
|
|
{
|
|
/// <summary>
|
|
/// This service is used to provide feedback to the user or developer about various system events.
|
|
/// </summary>
|
|
public static class LogService
|
|
{
|
|
#region Variables
|
|
|
|
private static readonly ILogger m_logger = new LogServiceLogger();
|
|
private static List<ILogger> m_loggers = new List<ILogger>();
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
static LogService()
|
|
{
|
|
if (Debugger.IsAttached)
|
|
{
|
|
// Add default loggers
|
|
AddLogger(new TraceLogger());
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
/// <summary>
|
|
/// Returns a logger that logs into the log service.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static ILogger Logger
|
|
{
|
|
get { return m_logger; }
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
/// <summary>
|
|
/// Logs an event.
|
|
/// </summary>
|
|
/// <param name="event">Event to log</param>
|
|
public static void Log(LogEvent @event)
|
|
{
|
|
m_loggers.ForEach(logger => logger.Log(@event));
|
|
}
|
|
|
|
#region LogDebugMessage
|
|
|
|
/// <summary>
|
|
/// Logs a message that is only visible in debug.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This is different than logging a normal message with debug importance as the call is optimised away in release.
|
|
/// </remarks>
|
|
/// <param name="message">Message formatting string. See <see cref="String.Format(string, object[])"/> for more information.</param>
|
|
/// <param name="args">Message format parameters. See <see cref="String.Format(string, object[])"/> for more information.</param>
|
|
[Conditional("DEBUG")]
|
|
public static void LogDebugMessage(string message, params object[] args)
|
|
{
|
|
LogDebugMessage(new Message(message, args) { Importance = MessageImportance.Debug });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Logs a message that is only visible in debug.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This is different than logging a normal message with debug importance as the call is optimised away in release.
|
|
/// </remarks>
|
|
/// <param name="message">Message to log.</param>
|
|
[Conditional("DEBUG")]
|
|
public static void LogDebugMessage(Message message)
|
|
{
|
|
LogMessage(message);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region LogMessage
|
|
|
|
/// <summary>
|
|
/// Logs a formatted message.
|
|
/// </summary>
|
|
/// <param name="format">Message formatting string. See <see cref="String.Format(string, object[])"/> for more information.</param>
|
|
/// <param name="args">Message format parameters. See <see cref="String.Format(string, object[])"/> for more information.</param>
|
|
public static void LogMessage(string format, params object[] args)
|
|
{
|
|
LogMessage(format, MessageImportance.Normal, args);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Logs a formatted message.
|
|
/// </summary>
|
|
/// <param name="message">Message formatting string. See <see cref="String.Format(string, object[])"/> for more information.</param>
|
|
/// <param name="messageImportance">Message importance.</param>
|
|
/// <param name="args">Message format parameters. See <see cref="String.Format(string, object[])"/> for more information.</param>
|
|
public static void LogMessage(string message, MessageImportance messageImportance, params object[] args)
|
|
{
|
|
LogMessage(new Message(message, args) { Importance = messageImportance });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Logs a message.
|
|
/// </summary>
|
|
/// <param name="message">Message to log.</param>
|
|
public static void LogMessage(Message message)
|
|
{
|
|
Log(message);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region LogWarning
|
|
|
|
/// <summary>
|
|
/// Logs a formatted warning.
|
|
/// </summary>
|
|
/// <param name="message">Message formatting string. See <see cref="String.Format(string, object[])"/> for more information.</param>
|
|
/// <param name="args">Message format parameters. See <see cref="String.Format(string, object[])"/> for more information.</param>
|
|
public static void LogWarning(string message, params object[] args)
|
|
{
|
|
Log(new Error(LogCategory.Warning, message, args));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Logs a warning.
|
|
/// </summary>
|
|
/// <param name="warning">Warning.</param>
|
|
public static void LogWarning(Error warning)
|
|
{
|
|
Log(warning);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region LogError
|
|
|
|
/// <summary>
|
|
/// Logs a formatted error.
|
|
/// </summary>
|
|
/// <param name="message">Message formatting string. See <see cref="String.Format(string, object[])"/> for more information.</param>
|
|
/// <param name="args">Message format parameters. See <see cref="String.Format(string, object[])"/> for more information.</param>
|
|
public static void LogError(string message, params object[] args)
|
|
{
|
|
Log(new Error(LogCategory.Error, message, args));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Logs an error.
|
|
/// </summary>
|
|
/// <param name="error">Error.</param>
|
|
public static void LogError(Error error)
|
|
{
|
|
Log(error);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Logs an exception.
|
|
/// </summary>
|
|
/// <param name="exception">Exception.</param>
|
|
/// <param name="message"></param>
|
|
/// <param name="args"></param>
|
|
public static void LogException(Exception exception, string message, params object[] args)
|
|
{
|
|
message = string.Format(message, args);
|
|
if (!message.EndsWith("."))
|
|
{
|
|
message += ".";
|
|
}
|
|
|
|
LogError("{0} Details : {1}.", message, exception);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Logs an exception.
|
|
/// </summary>
|
|
/// <param name="exception">Exception.</param>
|
|
public static void LogException(Exception exception)
|
|
{
|
|
LogException(exception, "An exception occured.");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
#region Loggers Management
|
|
|
|
/// <summary>
|
|
/// Adds a logger to the <see cref="LogService"/>.
|
|
/// </summary>
|
|
/// <param name="logger"></param>
|
|
public static void AddLogger(ILogger logger)
|
|
{
|
|
// Copy-on-write makes it all threadsafe (i.e. Log can be called from anywhere)
|
|
// However, it means that ILogger.Log can still be called for a while after RemoveLogger has been called.
|
|
|
|
Throw.IfNull(logger, "logger");
|
|
lock (m_loggers)
|
|
{
|
|
var newLoggers = m_loggers.ToList();
|
|
newLoggers.Add(logger);
|
|
m_loggers = newLoggers;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes a logger from the <see cref="LogService"/>.
|
|
/// </summary>
|
|
/// <param name="logger"></param>
|
|
public static void RemoveLogger(ILogger logger)
|
|
{
|
|
Throw.IfNull(logger, "logger");
|
|
lock (m_loggers)
|
|
{
|
|
var newLoggers = m_loggers.ToList();
|
|
newLoggers.Remove(logger);
|
|
m_loggers = newLoggers;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Nested Logger
|
|
|
|
/// <summary>
|
|
/// A logger that logs into the log service.
|
|
/// </summary>
|
|
private class LogServiceLogger : ILogger
|
|
{
|
|
void ILogger.Log(LogEvent @event)
|
|
{
|
|
Log(@event);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|