[CHANGE] Get parameters from framework logger.

This commit is contained in:
max 2024-11-30 18:55:09 +01:00
parent 448d85d6f6
commit e1cba8d68c
2 changed files with 8 additions and 6 deletions

View File

@ -4,6 +4,7 @@ namespace DotBased.Logging.MEL;
public class BasedLogger : Microsoft.Extensions.Logging.ILogger public class BasedLogger : Microsoft.Extensions.Logging.ILogger
{ {
private const string _messageTemplateKey = "{OriginalFormat}";
public BasedLogger(ILogger logger) public BasedLogger(ILogger logger)
{ {
basedLogger = logger; basedLogger = logger;
@ -22,24 +23,26 @@ public class BasedLogger : Microsoft.Extensions.Logging.ILogger
private LogCapsule ConstructCapsule<TState>(LogSeverity severity, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) private LogCapsule ConstructCapsule<TState>(LogSeverity severity, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{ {
//TODO: Extract parameters & format
var msgTemplate = string.Empty; var msgTemplate = string.Empty;
List<object?> templateParams = [];
if (state is IEnumerable<KeyValuePair<string, object>> stateEnum) if (state is IEnumerable<KeyValuePair<string, object>> stateEnum)
{ {
foreach (var prop in stateEnum) foreach (var prop in stateEnum)
{ {
if (prop is { Key: "{OriginalFormat}", Value: string propValueString }) if (prop is { Key: _messageTemplateKey, Value: string propValueString })
{ {
msgTemplate = propValueString; msgTemplate = propValueString;
continue;
} }
templateParams.Add(prop.Value);
} }
} }
return new LogCapsule() return new LogCapsule()
{ {
Exception = exception, Exception = exception,
Message = formatter.Invoke(state, exception), Message = msgTemplate,
Parameters = [], Parameters = templateParams.ToArray(),
Severity = severity, Severity = severity,
TimeStamp = DateTime.Now, TimeStamp = DateTime.Now,
Logger = basedLogger as LoggerBase ?? throw new NullReferenceException(nameof(basedLogger)) Logger = basedLogger as LoggerBase ?? throw new NullReferenceException(nameof(basedLogger))

View File

@ -4,11 +4,10 @@ namespace DotBased.Logging.MEL;
public static class LoggerBuilderExtensions 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) if (builder == null)
throw new ArgumentNullException(nameof(builder)); throw new ArgumentNullException(nameof(builder));
builder.AddProvider(new BasedLoggerProvider(options)); builder.AddProvider(new BasedLoggerProvider(options));
return builder;
} }
} }