mirror of
https://github.com/hmaxnl/DotBased.git
synced 2025-01-18 10:04:20 +01:00
Cleaned up LogService class
This commit is contained in:
parent
e338f91081
commit
cd28051032
|
@ -1,3 +1,4 @@
|
|||
using System.Collections.ObjectModel;
|
||||
using System.Reflection;
|
||||
|
||||
namespace DotBased.Logging;
|
||||
|
@ -7,28 +8,23 @@ namespace DotBased.Logging;
|
|||
/// </summary>
|
||||
public static class LogService
|
||||
{
|
||||
// TODO: Future: add middlewares and chanagable log processor
|
||||
static LogService()
|
||||
{
|
||||
Options = new LogOptions();
|
||||
_loggerSendEvent = LogProcessor.IncommingLogHandlerEvent;
|
||||
LoggerSendEvent = LogProcessor.IncommingLogHandlerEvent;
|
||||
}
|
||||
public static bool ShouldLog(LogSeverity maxSeverity, LogSeverity severity) => maxSeverity <= severity;
|
||||
public static LogOptions Options { get; private set; }
|
||||
public static LogProcessor LogProcessor { get; private set; } = new LogProcessor();
|
||||
|
||||
private static HashSet<LogAdapterBase> Adapters { get; } = new HashSet<LogAdapterBase>();
|
||||
private static HashSet<ILogger> Loggers { get; } = new HashSet<ILogger>();
|
||||
private static HashSet<LoggerBase> Loggers { get; } = new HashSet<LoggerBase>();
|
||||
|
||||
/// <summary>
|
||||
/// Internal communication between loggers and processor
|
||||
/// Action for internal communication between loggers and processor
|
||||
/// </summary>
|
||||
private static Action<LogCapsule> _loggerSendEvent;
|
||||
|
||||
public static void AddLogAdapter(LogAdapterBase logAdapter)
|
||||
{
|
||||
LogProcessor.LogProcessed += logAdapter.HandleLogEvent;
|
||||
Adapters.Add(logAdapter);
|
||||
}
|
||||
private static readonly Action<LogCapsule> LoggerSendEvent;
|
||||
|
||||
/// <summary>
|
||||
/// Register a logger that will be used in a class and will live as long as the class.
|
||||
|
@ -50,10 +46,39 @@ public static class LogService
|
|||
/// <returns>The configured <see cref="ILogger"/> implementation that will be configuered in the <see cref="LogOptions.LoggerBuilder"/> at the <see cref="LogService"/> class</returns>
|
||||
public static ILogger RegisterLogger(Type callerType)
|
||||
{
|
||||
var logger = Options.LoggerBuilder.Invoke(new CallerInformation(callerType), _loggerSendEvent);
|
||||
var logger = Options.LoggerBuilder.Invoke(new CallerInformation(callerType), LoggerSendEvent);
|
||||
Loggers.Add(logger);
|
||||
return logger;
|
||||
}
|
||||
|
||||
public static bool UnregisterLogger(LoggerBase logger) => Loggers.Remove(logger);
|
||||
|
||||
public static ReadOnlyCollection<LoggerBase> GetLoggers => new ReadOnlyCollection<LoggerBase>(Loggers.ToList());
|
||||
|
||||
/// <summary>
|
||||
/// Add a log adapter to the service.
|
||||
/// </summary>
|
||||
/// <param name="logAdapter">The log adapter based on <see cref="LogAdapterBase"/></param>
|
||||
public static void AddLogAdapter(LogAdapterBase logAdapter)
|
||||
{
|
||||
LogProcessor.LogProcessed += logAdapter.HandleLogEvent;
|
||||
Adapters.Add(logAdapter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the log adapter from the service.
|
||||
/// </summary>
|
||||
/// <param name="adapter">The adapter to remove</param>
|
||||
/// <returns>True if the adapter is succesfully removed otherwise false.</returns>
|
||||
public static bool RemoveLogAdapter(LogAdapterBase adapter)
|
||||
{
|
||||
if (!Adapters.Contains(adapter)) return false;
|
||||
LogProcessor.LogProcessed -= adapter.HandleLogEvent;
|
||||
return Adapters.Remove(adapter);
|
||||
}
|
||||
|
||||
public static ReadOnlyCollection<LogAdapterBase> GetAdapters =>
|
||||
new ReadOnlyCollection<LogAdapterBase>(Adapters.ToList());
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user