diff --git a/CLI/Program.cs b/CLI/Program.cs index 201c4d3..6e668b8 100644 --- a/CLI/Program.cs +++ b/CLI/Program.cs @@ -9,7 +9,7 @@ var serilogLogger = SetupSerilog(); LogService.AddLogAdapter(new SerilogAdapter(serilogLogger)); -var logger = LogService.RegisterLogger(nameof(Program)); +var logger = LogService.RegisterLogger(typeof(Program)); logger.Trace("Test TRACE log! {StringValue} {AnotherValue}", "WOW", "W0W"); logger.Debug("Test DEBUG log! {IntVal}", 69); diff --git a/DotBased.Logging.Serilog/SerilogAdapter.cs b/DotBased.Logging.Serilog/SerilogAdapter.cs index 88c36ec..4a41db8 100644 --- a/DotBased.Logging.Serilog/SerilogAdapter.cs +++ b/DotBased.Logging.Serilog/SerilogAdapter.cs @@ -15,7 +15,7 @@ public class SerilogAdapter : LogAdapterBase _messageTemplateParser = new MessageTemplateParser(); } - public const string SampleTemplate = "[{Timestamp:HH:mm:ss} - {Caller} -> {Source}] | {Level:u3}] {Message:lj}{NewLine}{Exception}"; + public const string SampleTemplate = "[{Timestamp:HH:mm:ss} - {Caller}] | {Level:u3}] {Message:lj}{NewLine}{Exception}"; private readonly global::Serilog.ILogger _serilogLogger; private readonly MessageTemplateParser _messageTemplateParser; @@ -24,10 +24,10 @@ public class SerilogAdapter : LogAdapterBase { if (capsule == null) return; - var baseLogger = capsule.Logger as Logger; var logger = _serilogLogger - .ForContext("Source", baseLogger?.Source.AssemblyName ?? "Static") - .ForContext("Caller", baseLogger?.Identifier); + .ForContext("Assembly", capsule.Logger.Caller.AssemblyName) + .ForContext("Source", capsule.Logger.Caller.Source) + .ForContext("Caller", capsule.Logger.Caller.Name); var template = _messageTemplateParser.Parse(capsule.Message); IEnumerable? properties = null; diff --git a/DotBased/Logging/ILogger.cs b/DotBased/Logging/ILogger.cs index 4adc402..7732999 100644 --- a/DotBased/Logging/ILogger.cs +++ b/DotBased/Logging/ILogger.cs @@ -1,7 +1,7 @@ namespace DotBased.Logging; /// -/// The ILogger interface for creating loggers that can be used by the +/// ILogger interface used by /// public interface ILogger { diff --git a/DotBased/Logging/LogCapsule.cs b/DotBased/Logging/LogCapsule.cs index b8a94f5..4c8e423 100644 --- a/DotBased/Logging/LogCapsule.cs +++ b/DotBased/Logging/LogCapsule.cs @@ -19,5 +19,5 @@ public class LogCapsule /// /// The logger that generated this capsule /// - public ILogger Logger { get; set; } + public LoggerBase Logger { get; set; } } \ No newline at end of file diff --git a/DotBased/Logging/LogOptions.cs b/DotBased/Logging/LogOptions.cs index 850d539..b2dd560 100644 --- a/DotBased/Logging/LogOptions.cs +++ b/DotBased/Logging/LogOptions.cs @@ -11,8 +11,8 @@ public class LogOptions public LogSeverity Severity { get; set; } = LogSeverity.Trace; /// - /// The function that will build and return the when calling , so a custom logger can be used. + /// The function that will build and return the when calling , so a custom logger based on the can be used. /// - public Func, ILogger> LoggerBuilder { get; set; } = - (identifier, source, sendEvent) => new Logger(identifier, source, ref sendEvent); + public Func, LoggerBase> LoggerBuilder { get; set; } = + (identifier, sendEvent) => new Logger(identifier, ref sendEvent); } \ No newline at end of file diff --git a/DotBased/Logging/LogService.cs b/DotBased/Logging/LogService.cs index 9822138..bee03a2 100644 --- a/DotBased/Logging/LogService.cs +++ b/DotBased/Logging/LogService.cs @@ -46,32 +46,34 @@ public static class LogService /// } /// /// - /// The identifier name of the logger, this will be passed to the log adapter as the source. + /// The type that called the function /// The configured implementation that will be configuered in the at the class - public static ILogger RegisterLogger(string identifier) + public static ILogger RegisterLogger(Type callerType) { - var asm = Assembly.GetCallingAssembly(); - var logger = Options.LoggerBuilder.Invoke(identifier, CallingSource.LoadFromAsm(asm), _loggerSendEvent); + var logger = Options.LoggerBuilder.Invoke(new CallerInformation(callerType), _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 readonly struct CallerInformation +{ + public CallerInformation(Type type) + { + Name = type.Name; + Source = type.FullName ?? type.GUID.ToString(); + Namespace = type.Namespace ?? string.Empty; + SourceAssembly = type.Assembly; + + var asmName = SourceAssembly.GetName(); + AssemblyName = asmName.Name ?? "Unknown"; + AssemblyFullname = asmName.FullName; + } + public string Name { get; } + public string Source { get; } + public string Namespace { get; } + public Assembly SourceAssembly { get; } public string AssemblyName { get; } - public string AssemblyFullName { get; set; } + public string AssemblyFullname { get; } } \ No newline at end of file diff --git a/DotBased/Logging/Logger.cs b/DotBased/Logging/Logger.cs index 8bccd56..cc8a293 100644 --- a/DotBased/Logging/Logger.cs +++ b/DotBased/Logging/Logger.cs @@ -1,21 +1,16 @@ namespace DotBased.Logging; /// -/// Main base logger, this class is the default logger that the function will return. +/// Main logger, this class is the default logger that the function will return. /// -public class Logger(string identifier, CallingSource source, ref Action logProcessorHandler) : ILogger +public class Logger(CallerInformation caller, ref Action logProcessorHandler) : LoggerBase(caller, ref logProcessorHandler) { - public string Identifier { get; } = identifier; - public CallingSource Source { get; } = source; - - private readonly Action _processLog = logProcessorHandler; - public void Log(LogCapsule capsule) { - _processLog(capsule); + ProcessLog(capsule); } - public void Trace(string message, params object?[]? parameters) + public override void Trace(string message, params object?[]? parameters) { Log(new LogCapsule() { @@ -27,7 +22,7 @@ public class Logger(string identifier, CallingSource source, ref Action HashCode.Combine(Identifier, Source.AssemblyFullName); + public override int GetHashCode() => HashCode.Combine(Caller.Source, Caller.AssemblyFullname); } \ No newline at end of file diff --git a/DotBased/Logging/LoggerBase.cs b/DotBased/Logging/LoggerBase.cs new file mode 100644 index 0000000..d9f4652 --- /dev/null +++ b/DotBased/Logging/LoggerBase.cs @@ -0,0 +1,20 @@ +namespace DotBased.Logging; + +/// +/// Base for creating loggers +/// +/// The caller information +/// The handler where the logs can be send to +public abstract class LoggerBase(CallerInformation caller, ref Action logProcessorHandler) : ILogger +{ + public CallerInformation Caller { get; } = caller; + + internal readonly Action ProcessLog = logProcessorHandler; + + public abstract void Trace(string message, params object?[]? parameters); + public abstract void Debug(string message, params object?[]? parameters); + public abstract void Information(string message, params object?[]? parameters); + public abstract void Warning(string message, params object?[]? parameters); + public abstract void Error(Exception exception, string message, params object?[]? parameters); + public abstract void Fatal(Exception exception, string message, params object?[]? parameters); +} \ No newline at end of file