Skip to content

Commit

Permalink
Fix: Splat.NLog - Faster with switch on Enum value, than dictionary l…
Browse files Browse the repository at this point in the history
…ookup (#1257)
  • Loading branch information
snakefoot authored Dec 31, 2024
1 parent 4329977 commit ddef2ed
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
46 changes: 22 additions & 24 deletions src/Splat.NLog/NLogLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;

Expand All @@ -15,17 +14,6 @@ namespace Splat.NLog;
[DebuggerDisplay("Name={_inner.Name} Level={Level}")]
public sealed class NLogLogger : IFullLogger, IDisposable
{
private static readonly KeyValuePair<LogLevel, global::NLog.LogLevel>[] _mappings =
[
new(LogLevel.Debug, global::NLog.LogLevel.Debug),
new(LogLevel.Info, global::NLog.LogLevel.Info),
new(LogLevel.Warn, global::NLog.LogLevel.Warn),
new(LogLevel.Error, global::NLog.LogLevel.Error),
new(LogLevel.Fatal, global::NLog.LogLevel.Fatal),
];

private static readonly ImmutableDictionary<LogLevel, global::NLog.LogLevel> _mappingsDictionary = _mappings.ToImmutableDictionary();

private readonly global::NLog.Logger _inner;

/// <summary>
Expand All @@ -47,34 +35,34 @@ public LogLevel Level
}

/// <inheritdoc />
public bool IsDebugEnabled => _inner.IsEnabled(global::NLog.LogLevel.Debug);
public bool IsDebugEnabled => _inner.IsDebugEnabled;

/// <inheritdoc />
public bool IsInfoEnabled => _inner.IsEnabled(global::NLog.LogLevel.Info);
public bool IsInfoEnabled => _inner.IsInfoEnabled;

/// <inheritdoc />
public bool IsWarnEnabled => _inner.IsEnabled(global::NLog.LogLevel.Warn);
public bool IsWarnEnabled => _inner.IsWarnEnabled;

/// <inheritdoc />
public bool IsErrorEnabled => _inner.IsEnabled(global::NLog.LogLevel.Error);
public bool IsErrorEnabled => _inner.IsErrorEnabled;

/// <inheritdoc />
public bool IsFatalEnabled => _inner.IsEnabled(global::NLog.LogLevel.Fatal);
public bool IsFatalEnabled => _inner.IsFatalEnabled;

/// <inheritdoc />
public void Dispose() => _inner.LoggerReconfigured -= OnInnerLoggerReconfigured;

/// <inheritdoc />
public void Write(string message, LogLevel logLevel) => _inner.Log(_mappingsDictionary[logLevel], message);
public void Write(string message, LogLevel logLevel) => _inner.Log(ResolveLogLevel(logLevel), message);

/// <inheritdoc />
public void Write(Exception exception, string message, LogLevel logLevel) => _inner.Log(_mappingsDictionary[logLevel], exception, message);
public void Write(Exception exception, string message, LogLevel logLevel) => _inner.Log(ResolveLogLevel(logLevel), exception, message);

/// <inheritdoc />
public void Write(string message, Type type, LogLevel logLevel) => LogResolver.Resolve(type).Log(_mappingsDictionary[logLevel], message);
public void Write(string message, Type type, LogLevel logLevel) => LogResolver.Resolve(type).Log(ResolveLogLevel(logLevel), message);

/// <inheritdoc />
public void Write(Exception exception, string message, Type type, LogLevel logLevel) => LogResolver.Resolve(type).Log(_mappingsDictionary[logLevel], exception, message);
public void Write(Exception exception, string message, Type type, LogLevel logLevel) => LogResolver.Resolve(type).Log(ResolveLogLevel(logLevel), exception, message);

/// <inheritdoc/>
public void Debug<TArgument>(string message, TArgument args) => _inner.Debug(CultureInfo.InvariantCulture, message, args);
Expand Down Expand Up @@ -556,6 +544,16 @@ public LogLevel Level
/// <inheritdoc/>
public void Fatal<TArgument1, TArgument2, TArgument3, TArgument4, TArgument5, TArgument6, TArgument7, TArgument8, TArgument9, TArgument10>(Exception exception, string messageFormat, TArgument1 argument1, TArgument2 argument2, TArgument3 argument3, TArgument4 argument4, TArgument5 argument5, TArgument6 argument6, TArgument7 argument7, TArgument8 argument8, TArgument9 argument9, TArgument10 argument10) => _inner.Fatal(exception, messageFormat, argument1, argument2, argument3, argument4, argument5, argument6, argument7, argument8, argument9, argument10);

private static global::NLog.LogLevel ResolveLogLevel(LogLevel logLevel) => logLevel switch
{
LogLevel.Debug => global::NLog.LogLevel.Debug,
LogLevel.Info => global::NLog.LogLevel.Info,
LogLevel.Warn => global::NLog.LogLevel.Warn,
LogLevel.Error => global::NLog.LogLevel.Error,
LogLevel.Fatal => global::NLog.LogLevel.Fatal,
_ => throw new ArgumentException($"Unknown LogLevel {logLevel}", nameof(logLevel)),
};

private void OnInnerLoggerReconfigured(object? sender, EventArgs e) => SetLogLevel();

/// <summary>
Expand All @@ -566,11 +564,11 @@ public LogLevel Level
/// </remarks>
private void SetLogLevel()
{
foreach (var mapping in _mappings)
foreach (LogLevel logLevel in Enum.GetValues(typeof(LogLevel)))
{
if (_inner.IsEnabled(mapping.Value))
if (_inner.IsEnabled(ResolveLogLevel(logLevel)))
{
Level = mapping.Key;
Level = logLevel;
return;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/Splat.NLog/Splat.NLog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog" />
<PackageReference Include="System.Collections.Immutable" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Splat\Splat.csproj" />
Expand Down
4 changes: 2 additions & 2 deletions src/Splat/Logging/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public interface ILogger
void Write(Exception exception, [Localizable(false)] string message, LogLevel logLevel);

/// <summary>
/// Writes a messge to the target.
/// Writes a message to the target.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="type">The type.</param>
/// <param name="logLevel">The log level.</param>
void Write([Localizable(false)] string message, [Localizable(false)] Type type, LogLevel logLevel);

/// <summary>
/// Writes a messge to the target.
/// Writes a message to the target.
/// </summary>
/// <param name="exception">The exception that occured.</param>
/// <param name="message">The message.</param>
Expand Down
4 changes: 2 additions & 2 deletions src/Splat/Logging/IStaticFullLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public interface IStaticFullLogger
void Write(Exception exception, [Localizable(false)] string message, LogLevel logLevel, [CallerMemberName]string? callerMemberName = null);

/// <summary>
/// Writes a messge to the target.
/// Writes a message to the target.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="type">The type.</param>
Expand All @@ -350,7 +350,7 @@ public interface IStaticFullLogger
void Write([Localizable(false)] string message, [Localizable(false)] Type type, LogLevel logLevel, [CallerMemberName]string? callerMemberName = null);

/// <summary>
/// Writes a messge to the target.
/// Writes a message to the target.
/// </summary>
/// <param name="exception">The exception that occured.</param>
/// <param name="message">The message.</param>
Expand Down

0 comments on commit ddef2ed

Please sign in to comment.