Skip to content

Commit

Permalink
NLogLogger - Optimize logging of LogMessage when Parameters() is obje…
Browse files Browse the repository at this point in the history
…ct-array
  • Loading branch information
snakefoot committed Oct 14, 2024
1 parent 52dbbb3 commit 5b21104
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
8 changes: 7 additions & 1 deletion src/Akka.Logger.NLog/NLogLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private static void LogEvent(NLogger logger, NLogLevel level, Exception exceptio
return;

var logEventInfo = logEvent.Message is LogMessage logMessage ?
new LogEventInfo(level, logger.Name, null, logMessage.Format, logMessage.Parameters().ToArray(), exception) :
new LogEventInfo(level, logger.Name, null, logMessage.Format, GetLogMessageParameterArray(logMessage), exception) :
new LogEventInfo(level, logger.Name, null, "{0}", new object[] { logEvent.Message.ToString() }, exception);
if (logEventInfo.TimeStamp.Kind == logEvent.Timestamp.Kind)
logEventInfo.TimeStamp = logEvent.Timestamp; // Timestamp of original LogEvent (instead of async Logger thread timestamp)
Expand All @@ -66,5 +66,11 @@ private static void LogEvent(NLogger logger, NLogLevel level, Exception exceptio
logEventInfo.Properties["threadId"] = logEvent.Thread.ManagedThreadId; // ThreadId of the original LogEvent (instead of async Logger threadid)
logger.Log(logEventInfo);
}

private static object[] GetLogMessageParameterArray(LogMessage logMessage)
{
var parameters = logMessage.Parameters();
return parameters is object[] parameterArray ? parameterArray : parameters?.ToArray();
}
}
}
21 changes: 10 additions & 11 deletions src/Akka.Logger.NLog/NLogMessageFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@

namespace Akka.Logger.NLog
{
/// <inheritdoc />
/// <summary>
/// This class contains methods used to convert MessageTemplated messages
/// into normal text messages.
/// </summary>
/// <remarks>
/// You need to enable the Akka.Logger.NLog.NLogMessageFormatter across your entire ActorSystem - this will replace Akka.NET's default ILogMessageFormatter with NLog's.
///
/// You can accomplish this by setting the akka.logger-formatter setting like below:
/// <code>
/// akka.logger-formatter="Akka.Logger.NLog.NLogMessageFormatter, Akka.Logger.NLog"
/// </code>
/// </remarks>
public class NLogMessageFormatter : ILogMessageFormatter
{
/// <summary>
/// Converts the specified template string to a text string using the specified
/// token array to match replacements.
/// </summary>
/// <param name="format">The template string used in the conversion.</param>
/// <param name="args">The array that contains values to replace in the template.</param>
/// <returns>
/// A text string where the template placeholders have been replaced with
/// their corresponding values.
/// </returns>
/// <inheritdoc />
public string Format(string format, params object[] args)
{
if (args?.Length > 0)
Expand All @@ -32,6 +30,7 @@ public string Format(string format, params object[] args)
return format;
}

/// <inheritdoc />
public string Format(string format, IEnumerable<object> args)
=> Format(format, args.ToArray());
}
Expand Down

0 comments on commit 5b21104

Please sign in to comment.