mirror of
https://github.com/hmaxnl/DotBased.git
synced 2025-01-18 10:04:20 +01:00
[CHANGE] Get parameters from framework logger.
This commit is contained in:
parent
448d85d6f6
commit
e1cba8d68c
|
@ -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<TState>(LogSeverity severity, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
|
||||
{
|
||||
//TODO: Extract parameters & format
|
||||
var msgTemplate = string.Empty;
|
||||
List<object?> templateParams = [];
|
||||
if (state is IEnumerable<KeyValuePair<string, object>> 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))
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user