Skip to content

Commit

Permalink
Splat.NLog - Reduce allocation when creating new NLogLogger (#1258)
Browse files Browse the repository at this point in the history
* Splat.NLog - Reduce allocation when creating new NLogLogger

* Use generic Enum.GetValues available from NET5 and newer
  • Loading branch information
snakefoot authored Dec 31, 2024
1 parent ddef2ed commit b14c4bc
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Splat.NLog/NLogLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ namespace Splat.NLog;
[DebuggerDisplay("Name={_inner.Name} Level={Level}")]
public sealed class NLogLogger : IFullLogger, IDisposable
{
#if NET5_0_OR_GREATER
private static readonly LogLevel[] _allLogLevels = Enum.GetValues<LogLevel>();
#else
private static readonly LogLevel[] _allLogLevels = Enum.GetValues(typeof(LogLevel)).Cast<LogLevel>().ToArray();
#endif
private readonly global::NLog.Logger _inner;

/// <summary>
Expand Down Expand Up @@ -564,7 +569,7 @@ public LogLevel Level
/// </remarks>
private void SetLogLevel()
{
foreach (LogLevel logLevel in Enum.GetValues(typeof(LogLevel)))
foreach (LogLevel logLevel in _allLogLevels)
{
if (_inner.IsEnabled(ResolveLogLevel(logLevel)))
{
Expand Down

0 comments on commit b14c4bc

Please sign in to comment.