diff --git a/src/NLog.Extensions.Logging/Logging/NLogLogger.cs b/src/NLog.Extensions.Logging/Logging/NLogLogger.cs index 5cef8809..f9dd666f 100644 --- a/src/NLog.Extensions.Logging/Logging/NLogLogger.cs +++ b/src/NLog.Extensions.Logging/Logging/NLogLogger.cs @@ -42,6 +42,15 @@ public void Log(Microsoft.Extensions.Logging.LogLevel logLevel, EventId throw new ArgumentNullException(nameof(formatter)); } + LogEventInfo eventInfo = CreateLogEventInfo(nLogLogLevel, state, exception, formatter); + + CaptureEventId(eventInfo, eventId); + + _logger.Log(typeof(Microsoft.Extensions.Logging.ILogger), eventInfo); + } + + private LogEventInfo CreateLogEventInfo(LogLevel nLogLogLevel, TState state, Exception exception, Func formatter) + { var messageProperties = (_options.CaptureMessageTemplates || _options.CaptureMessageProperties) ? state as IReadOnlyList> : null; @@ -50,56 +59,23 @@ public void Log(Microsoft.Extensions.Logging.LogLevel logLevel, EventId ? NLogMessageParameterList.TryParse(messageProperties) : null; + string formattedMesage = null; LogEventInfo eventInfo = TryParseMessageTemplate(nLogLogLevel, messageProperties, messageParameters) ?? - CreateLogEventInfo(nLogLogLevel, formatter(state, exception), messageProperties, messageParameters); + TryCaptureMessageTemplate(nLogLogLevel, formattedMesage ?? (formattedMesage = formatter(state, exception)), messageProperties, messageParameters) ?? + CreateSimpleLogEventInfo(nLogLogLevel, formattedMesage, messageProperties, messageParameters); - if (exception != null) + if (messageParameters == null && messageProperties == null && _options.CaptureMessageProperties) { - eventInfo.Exception = exception; + CaptureMessageProperties(eventInfo, state); } - if (messageParameters == null && _options.CaptureMessageProperties) + if (exception != null) { - if (!CaptureMessagePropertiesList(eventInfo, messageProperties)) - { - CaptureMessageProperties(eventInfo, state); - } + eventInfo.Exception = exception; } - CaptureEventId(eventInfo, eventId); - - _logger.Log(typeof(Microsoft.Extensions.Logging.ILogger), eventInfo); - } - - - private LogEventInfo CreateLogEventInfo(LogLevel nLogLogLevel, string message, IReadOnlyList> messageProperties, NLogMessageParameterList messageParameters) - { - if (messageParameters?.HasComplexParameters == false) - { - // Parsing not needed, we take the fast route - var originalMessage = messageParameters.GetOriginalMessage(messageProperties); - var eventInfo = new LogEventInfo(nLogLogLevel, _logger.Name, originalMessage ?? message, messageParameters.IsPositional ? _emptyParameterArray : messageParameters); - if (originalMessage != null) - { - SetLogEventMessageFormatter(eventInfo, messageParameters, message); - } - return eventInfo; - } - else - { - // Parsing failed or no messageParameters - var eventInfo = LogEventInfo.Create(nLogLogLevel, _logger.Name, message); - if (messageParameters?.Count > 0) - { - for (int i = 0; i < messageParameters.Count; ++i) - { - var property = messageParameters[i]; - eventInfo.Properties[property.Name] = property.Value; - } - } - return eventInfo; - } + return eventInfo; } /// @@ -136,6 +112,41 @@ private LogEventInfo TryParseMessageTemplate(LogLevel nLogLogLevel, IReadOnlyLis return null; // Parsing not needed } + private LogEventInfo TryCaptureMessageTemplate(LogLevel nLogLogLevel, string message, IReadOnlyList> messageProperties, NLogMessageParameterList messageParameters) + { + if (messageParameters?.HasComplexParameters == false) + { + // Parsing not needed, we take the fast route + var originalMessage = messageParameters.GetOriginalMessage(messageProperties); + var eventInfo = new LogEventInfo(nLogLogLevel, _logger.Name, originalMessage ?? message, messageParameters.IsPositional ? _emptyParameterArray : messageParameters); + if (originalMessage != null) + { + SetLogEventMessageFormatter(eventInfo, messageParameters, message); + } + return eventInfo; + } + return null; + } + + private LogEventInfo CreateSimpleLogEventInfo(LogLevel nLogLogLevel, string message, IReadOnlyList> messageProperties, NLogMessageParameterList messageParameters) + { + // Parsing failed or no messageParameters + var eventInfo = LogEventInfo.Create(nLogLogLevel, _logger.Name, message); + if (messageParameters != null) + { + for (int i = 0; i < messageParameters.Count; ++i) + { + var property = messageParameters[i]; + eventInfo.Properties[property.Name] = property.Value; + } + } + else if (messageProperties != null && _options.CaptureMessageProperties) + { + CaptureMessagePropertiesList(eventInfo, messageProperties); + } + return eventInfo; + } + /// /// Allocates object[]-array for after checking /// for mismatch between Microsoft Extension Logging and NLog Message Template Parser