Skip to content

Commit

Permalink
NLogLogger - Move some complexity out of Log-method
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Aug 1, 2018
1 parent 728870a commit 25b231d
Showing 1 changed file with 52 additions and 41 deletions.
93 changes: 52 additions & 41 deletions src/NLog.Extensions.Logging/Logging/NLogLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ public void Log<TState>(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<TState>(LogLevel nLogLogLevel, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
var messageProperties = (_options.CaptureMessageTemplates || _options.CaptureMessageProperties)
? state as IReadOnlyList<KeyValuePair<string, object>>
: null;
Expand All @@ -50,56 +59,23 @@ public void Log<TState>(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<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;
}
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;
}

/// <summary>
Expand Down Expand Up @@ -136,6 +112,41 @@ private LogEventInfo TryParseMessageTemplate(LogLevel nLogLogLevel, IReadOnlyLis
return null; // Parsing not needed
}

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;
}

private LogEventInfo CreateSimpleLogEventInfo(LogLevel nLogLogLevel, string message, IReadOnlyList<KeyValuePair<string, object>> 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;
}

/// <summary>
/// Allocates object[]-array for <see cref="LogEventInfo.Parameters"/> after checking
/// for mismatch between Microsoft Extension Logging and NLog Message Template Parser
Expand Down

0 comments on commit 25b231d

Please sign in to comment.