Cleaned up LogService class

This commit is contained in:
Max 2024-05-04 16:24:14 +02:00
parent e338f91081
commit cd28051032

View File

@ -1,3 +1,4 @@
using System.Collections.ObjectModel;
using System.Reflection; using System.Reflection;
namespace DotBased.Logging; namespace DotBased.Logging;
@ -7,28 +8,23 @@ namespace DotBased.Logging;
/// </summary> /// </summary>
public static class LogService public static class LogService
{ {
// TODO: Future: add middlewares and chanagable log processor
static LogService() static LogService()
{ {
Options = new LogOptions(); Options = new LogOptions();
_loggerSendEvent = LogProcessor.IncommingLogHandlerEvent; LoggerSendEvent = LogProcessor.IncommingLogHandlerEvent;
} }
public static bool ShouldLog(LogSeverity maxSeverity, LogSeverity severity) => maxSeverity <= severity; public static bool ShouldLog(LogSeverity maxSeverity, LogSeverity severity) => maxSeverity <= severity;
public static LogOptions Options { get; private set; } public static LogOptions Options { get; private set; }
public static LogProcessor LogProcessor { get; private set; } = new LogProcessor(); public static LogProcessor LogProcessor { get; private set; } = new LogProcessor();
private static HashSet<LogAdapterBase> Adapters { get; } = new HashSet<LogAdapterBase>(); 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> /// <summary>
/// Internal communication between loggers and processor /// Action for internal communication between loggers and processor
/// </summary> /// </summary>
private static Action<LogCapsule> _loggerSendEvent; private static readonly Action<LogCapsule> LoggerSendEvent;
public static void AddLogAdapter(LogAdapterBase logAdapter)
{
LogProcessor.LogProcessed += logAdapter.HandleLogEvent;
Adapters.Add(logAdapter);
}
/// <summary> /// <summary>
/// Register a logger that will be used in a class and will live as long as the class. /// 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> /// <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) 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); Loggers.Add(logger);
return 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());
} }