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

Splat.NLog - Faster mapping of LogLevel using switch-operation #1257

Merged
merged 1 commit into from
Dec 31, 2024
Merged
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
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 @@
[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 @@
}

/// <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 @@
/// <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)),

Check warning on line 554 in src/Splat.NLog/NLogLogger.cs

View check run for this annotation

Codecov / codecov/patch

src/Splat.NLog/NLogLogger.cs#L554

Added line #L554 was not covered by tests
};

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

/// <summary>
Expand All @@ -566,11 +564,11 @@
/// </remarks>
private void SetLogLevel()
{
foreach (var mapping in _mappings)
foreach (LogLevel logLevel in Enum.GetValues(typeof(LogLevel)))

Check warning on line 567 in src/Splat.NLog/NLogLogger.cs

View workflow job for this annotation

GitHub Actions / build / build

Prefer the generic overload 'System.Enum.GetValues<TEnum>()' instead of 'System.Enum.GetValues(System.Type)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2263)

Check warning on line 567 in src/Splat.NLog/NLogLogger.cs

View workflow job for this annotation

GitHub Actions / build / build

Prefer the generic overload 'System.Enum.GetValues<TEnum>()' instead of 'System.Enum.GetValues(System.Type)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2263)
{
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
Loading