using System.Reflection;
namespace DotBased.Logging;
///
/// Main log service class, handles the loggers, log processor and adapters.
///
public static class LogService
{
static LogService()
{
Options = new LogOptions();
_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();
///
/// Internal communication between loggers and processor
///
private static Action _loggerSendEvent;
public static void AddLogAdapter(LogAdapterBase logAdapter)
{
LogProcessor.LogProcessed += logAdapter.HandleLogEvent;
Adapters.Add(logAdapter);
}
///
/// Register a logger that will be used in a class and will live as long as the class.
/// This will get the calling assembly and will pass that through ther log adapters.
///
///
///
/// public class Program
/// {
/// public Program
/// {
/// logger = LogService.RegisterLogger(nameof(Program));
/// }
/// private ILogger logger;
/// }
///
///
/// At the moment this function will only return the default class, this is not configureble at the moment!
/// The identifier name of the logger, this will be passed to the log adapter as the source.
/// The configured implementation that will be configuered in the at the class
public static ILogger RegisterLogger(string identifier)
{
var asm = Assembly.GetCallingAssembly();
var logger = new Logger(identifier, CallingSource.LoadFromAsm(asm), ref _loggerSendEvent);
Loggers.Add(logger);
return logger;
}
}
///
/// Data struct for holding calling source information.
///
public struct CallingSource
{
private CallingSource(Assembly asm)
{
AssemblySource = asm;
var asmName = AssemblySource.GetName();
AssemblyName = asmName.Name ?? "Unknown";
AssemblyFullName = asmName.FullName;
}
public static CallingSource LoadFromAsm(Assembly asm) => new CallingSource(asm);
public Assembly AssemblySource { get; }
public string AssemblyName { get; }
public string AssemblyFullName { get; set; }
}