From e1cba8d68c560ae8142a1ac40ccaff2506fb76c8 Mon Sep 17 00:00:00 2001 From: max Date: Sat, 30 Nov 2024 18:55:09 +0100 Subject: [PATCH] [CHANGE] Get parameters from framework logger. --- DotBased.Logging.MEL/BasedLogger.cs | 11 +++++++---- DotBased.Logging.MEL/LoggerBuilderExtensions.cs | 3 +-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/DotBased.Logging.MEL/BasedLogger.cs b/DotBased.Logging.MEL/BasedLogger.cs index 6c0f422..39574a1 100644 --- a/DotBased.Logging.MEL/BasedLogger.cs +++ b/DotBased.Logging.MEL/BasedLogger.cs @@ -4,6 +4,7 @@ namespace DotBased.Logging.MEL; public class BasedLogger : Microsoft.Extensions.Logging.ILogger { + private const string _messageTemplateKey = "{OriginalFormat}"; public BasedLogger(ILogger logger) { basedLogger = logger; @@ -22,24 +23,26 @@ public class BasedLogger : Microsoft.Extensions.Logging.ILogger private LogCapsule ConstructCapsule(LogSeverity severity, EventId eventId, TState state, Exception? exception, Func formatter) { - //TODO: Extract parameters & format var msgTemplate = string.Empty; + List templateParams = []; if (state is IEnumerable> stateEnum) { foreach (var prop in stateEnum) { - if (prop is { Key: "{OriginalFormat}", Value: string propValueString }) + if (prop is { Key: _messageTemplateKey, Value: string propValueString }) { msgTemplate = propValueString; + continue; } + templateParams.Add(prop.Value); } } return new LogCapsule() { Exception = exception, - Message = formatter.Invoke(state, exception), - Parameters = [], + Message = msgTemplate, + Parameters = templateParams.ToArray(), Severity = severity, TimeStamp = DateTime.Now, Logger = basedLogger as LoggerBase ?? throw new NullReferenceException(nameof(basedLogger)) diff --git a/DotBased.Logging.MEL/LoggerBuilderExtensions.cs b/DotBased.Logging.MEL/LoggerBuilderExtensions.cs index ddde1d6..6ce70f5 100644 --- a/DotBased.Logging.MEL/LoggerBuilderExtensions.cs +++ b/DotBased.Logging.MEL/LoggerBuilderExtensions.cs @@ -4,11 +4,10 @@ namespace DotBased.Logging.MEL; public static class LoggerBuilderExtensions { - public static ILoggingBuilder AddDotBased(this ILoggingBuilder builder, LogOptions options) + public static void AddDotBasedLoggerProvider(this ILoggingBuilder builder, LogOptions options) { if (builder == null) throw new ArgumentNullException(nameof(builder)); builder.AddProvider(new BasedLoggerProvider(options)); - return builder; } } \ No newline at end of file