[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
{
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))