Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NLogLogger - Optimize logging of LogMessage when Parameters() is object-array #186

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Akka.Logger.NLog.Tests/Akka.Logger.NLog.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Akka.TestKit.Xunit2" />
</ItemGroup>

Expand Down
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
7 changes: 3 additions & 4 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
</ItemGroup>
<!-- Test dependencies -->
<ItemGroup>
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageVersion Include="xunit" Version="2.5.1" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="Akka.TestKit.Xunit2" Version="$(AkkaVersion)" />
</ItemGroup>
<!-- SourceLink support for all Akka.NET projects -->
<ItemGroup>
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
</ItemGroup>
</Project>