private static void SetLogEventMessageFormatter(LogEventInfo logEvent, NLogMessageParameterList messageTemplateParameters, string formattedMessage) { var parameters = new object[messageTemplateParameters.Count + 1]; for (int i = 0; i < parameters.Length - 1; ++i) { parameters[i] = messageTemplateParameters[i].Value; } parameters[parameters.Length - 1] = formattedMessage; logEvent.Parameters = parameters; logEvent.MessageFormatter = (l) => (string)l.Parameters[l.Parameters.Length - 1]; }
/// <summary> /// Create Log Event with multiple parameters (last parameter is the {OriginalFormat}) /// </summary> private LogEventInfo CreateLogEventInfoWithMultipleParameters(NLog.LogLevel nLogLogLevel, string message, IReadOnlyList <KeyValuePair <string, object> > parameterList) { var messageTemplateParameters = new NLogMessageParameterList(parameterList); var originalMessage = messageTemplateParameters.OriginalMessage as string; var logEvent = new LogEventInfo(nLogLogLevel, _logger.Name, originalMessage ?? message, messageTemplateParameters); if (originalMessage != null) { SetLogEventMessageFormatter(logEvent, messageTemplateParameters, message); } return(logEvent); }