private LogEventInfo CaptureMessageTemplate(LogLevel nLogLogLevel, string message, IReadOnlyList <KeyValuePair <string, object> > messageProperties, NLogMessageParameterList messageParameters)
        {
            // Parsing not needed, we take the fast route
            var originalMessage = messageParameters.GetOriginalMessage(messageProperties) ?? message;
            var logEvent        = new LogEventInfo(nLogLogLevel, _logger.Name, originalMessage, messageParameters.IsPositional ? EmptyParameterArray : messageParameters);

            if (!ReferenceEquals(originalMessage, message))
            {
                SetLogEventMessageFormatter(logEvent, messageParameters, message);
            }
            return(logEvent);
        }
Exemple #2
0
 private LogEventInfo TryCaptureMessageTemplate(LogLevel nLogLogLevel, string message, IReadOnlyList <KeyValuePair <string, object> > 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);
 }
        /// <summary>
        /// Checks if the already parsed input message-parameters must be sent through
        /// the NLog MessageTemplate Parser for proper handling of message-template syntax (Ex. @)
        /// </summary>
        /// <remarks>
        /// Using the NLog MessageTemplate Parser will hurt performance: 1 x Microsoft Parser - 2 x NLog Parser - 1 x NLog Formatter
        /// </remarks>
        private LogEventInfo TryParseMessageTemplate(LogLevel nLogLogLevel, IReadOnlyList <KeyValuePair <string, object> > messageProperties, NLogMessageParameterList messageParameters)
        {
            if (messageParameters?.HasMessageTemplateSyntax(_options.ParseMessageTemplates) == true)
            {
                var originalMessage           = messageParameters.GetOriginalMessage(messageProperties);
                var logEvent                  = new LogEventInfo(nLogLogLevel, _logger.Name, null, originalMessage, SingleItemArray);
                var messageTemplateParameters = logEvent.MessageTemplateParameters;   // Forces parsing of OriginalMessage
                if (messageTemplateParameters.Count > 0)
                {
                    // We have parsed the message and found parameters, now we need to do the parameter mapping
                    CaptureLogEventInfoParameters(logEvent, messageParameters, messageTemplateParameters);
                    return(logEvent);
                }

                return(null);    // Parsing not possible
            }

            return(null);    // Parsing not needed
        }
Exemple #4
0
        /// <summary>
        /// Checks if the already parsed input message-parameters must be sent through
        /// the NLog MessageTemplate Parser for proper handling of message-template syntax.
        /// </summary>
        /// <remarks>
        /// Using the NLog MesageTemplate Parser will hurt performance: 1 x Microsoft Parser - 2 x NLog Parser - 1 x NLog Formatter
        /// </remarks>
        private LogEventInfo TryParseMessageTemplate(LogLevel nLogLogLevel, IReadOnlyList <KeyValuePair <string, object> > messageProperties, out NLogMessageParameterList messageParameters)
        {
            messageParameters = TryParseMessageParameterList(messageProperties);

            if (messageParameters?.HasMessageTemplateSyntax(_options.ParseMessageTemplates) == true)
            {
                var originalMessage            = messageParameters.GetOriginalMessage(messageProperties);
                var eventInfo                  = new LogEventInfo(nLogLogLevel, _logger.Name, null, originalMessage, _singleItemArray);
                var messagetTemplateParameters = eventInfo.MessageTemplateParameters;   // Forces parsing of OriginalMessage
                if (messagetTemplateParameters.Count > 0)
                {
                    // We have parsed the message and found parameters, now we need to do the parameter mapping
                    eventInfo.Parameters = CreateLogEventInfoParameters(messageParameters, messagetTemplateParameters, out var extraProperties);
                    AddExtraPropertiesToLogEvent(eventInfo, extraProperties);
                    return(eventInfo);
                }

                return(null);    // Parsing not possible
            }

            return(null);    // Parsing not needed
        }