From cd28051032ba09899eaf01eb9d969137c282399f Mon Sep 17 00:00:00 2001 From: Max <51083570+DRdrProfessor@users.noreply.github.com> Date: Sat, 4 May 2024 16:24:14 +0200 Subject: [PATCH] Cleaned up LogService class --- DotBased/Logging/LogService.cs | 47 ++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/DotBased/Logging/LogService.cs b/DotBased/Logging/LogService.cs index bee03a2..bc5666f 100644 --- a/DotBased/Logging/LogService.cs +++ b/DotBased/Logging/LogService.cs @@ -1,3 +1,4 @@ +using System.Collections.ObjectModel; using System.Reflection; namespace DotBased.Logging; @@ -7,28 +8,23 @@ namespace DotBased.Logging; /// 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 Adapters { get; } = new HashSet(); - private static HashSet Loggers { get; } = new HashSet(); + private static HashSet Loggers { get; } = new HashSet(); /// - /// Internal communication between loggers and processor + /// Action for internal communication between loggers and processor /// - private static Action _loggerSendEvent; - - public static void AddLogAdapter(LogAdapterBase logAdapter) - { - LogProcessor.LogProcessed += logAdapter.HandleLogEvent; - Adapters.Add(logAdapter); - } + private static readonly Action LoggerSendEvent; /// /// 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 /// The configured implementation that will be configuered in the at the class 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 GetLoggers => new ReadOnlyCollection(Loggers.ToList()); + + /// + /// Add a log adapter to the service. + /// + /// The log adapter based on + public static void AddLogAdapter(LogAdapterBase logAdapter) + { + LogProcessor.LogProcessed += logAdapter.HandleLogEvent; + Adapters.Add(logAdapter); + } + + /// + /// Removes the log adapter from the service. + /// + /// The adapter to remove + /// True if the adapter is succesfully removed otherwise false. + public static bool RemoveLogAdapter(LogAdapterBase adapter) + { + if (!Adapters.Contains(adapter)) return false; + LogProcessor.LogProcessed -= adapter.HandleLogEvent; + return Adapters.Remove(adapter); + } + + public static ReadOnlyCollection GetAdapters => + new ReadOnlyCollection(Adapters.ToList()); }