Skip to content

Commit

Permalink
Avoid unnecessary boxing in LogHelper (#2161)
Browse files Browse the repository at this point in the history
* Avoid unnecessary boxing in LogHelper

* Update src/Microsoft.IdentityModel.Logging/LogHelper.cs
  • Loading branch information
stephentoub authored and Brent Schmaltz committed Sep 6, 2023
1 parent 25b1287 commit 4cdb715
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/Microsoft.IdentityModel.Logging/LogHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
// Licensed under the MIT License.

using System;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.IdentityModel.Abstractions;

namespace Microsoft.IdentityModel.Logging
Expand Down Expand Up @@ -260,9 +258,9 @@ public static Exception LogExceptionMessage(EventLevel eventLevel, Exception exc
if (IdentityModelEventSource.Logger.IsEnabled() && IdentityModelEventSource.Logger.LogLevel >= eventLevel)
IdentityModelEventSource.Logger.Write(eventLevel, exception.InnerException, exception.Message);

EventLogLevel eventLogLevel = Enum.IsDefined(typeof(EventLogLevel), (int)eventLevel) ? (EventLogLevel)eventLevel : EventLogLevel.Error;
EventLogLevel eventLogLevel = EventLevelToEventLogLevel(eventLevel);
if (Logger.IsEnabled(eventLogLevel))
Logger.Log(WriteEntry((EventLogLevel)eventLevel, exception.InnerException, exception.Message, null));
Logger.Log(WriteEntry(eventLogLevel, exception.InnerException, exception.Message, null));

return exception;
}
Expand All @@ -277,8 +275,8 @@ public static void LogInformation(string message, params object[] args)
if (IdentityModelEventSource.Logger.IsEnabled() && IdentityModelEventSource.Logger.LogLevel >= EventLevel.Informational)
IdentityModelEventSource.Logger.WriteInformation(message, args);

if (Enum.IsDefined(typeof(EventLogLevel), (int)EventLevel.Informational) && Logger.IsEnabled((EventLogLevel)EventLevel.Informational))
Logger.Log(WriteEntry((EventLogLevel)EventLevel.Informational, null, message, args));
if (Logger.IsEnabled(EventLogLevel.Informational))
Logger.Log(WriteEntry(EventLogLevel.Informational, null, message, args));
}

/// <summary>
Expand All @@ -291,8 +289,8 @@ public static void LogVerbose(string message, params object[] args)
if (IdentityModelEventSource.Logger.IsEnabled())
IdentityModelEventSource.Logger.WriteVerbose(message, args);

if (Enum.IsDefined(typeof(EventLogLevel), (int)EventLevel.Verbose) && Logger.IsEnabled((EventLogLevel)EventLevel.Verbose))
Logger.Log(WriteEntry((EventLogLevel)EventLevel.Verbose, null, message, args));
if (Logger.IsEnabled(EventLogLevel.Verbose))
Logger.Log(WriteEntry(EventLogLevel.Verbose, null, message, args));
}

/// <summary>
Expand All @@ -305,8 +303,8 @@ public static void LogWarning(string message, params object[] args)
if (IdentityModelEventSource.Logger.IsEnabled())
IdentityModelEventSource.Logger.WriteWarning(message, args);

if (Enum.IsDefined(typeof(EventLogLevel), (int)EventLevel.Warning) && Logger.IsEnabled((EventLogLevel)EventLevel.Warning))
Logger.Log(WriteEntry((EventLogLevel)EventLevel.Warning, null, message, args));
if (Logger.IsEnabled(EventLogLevel.Warning))
Logger.Log(WriteEntry(EventLogLevel.Warning, null, message, args));
}

/// <summary>
Expand All @@ -319,8 +317,7 @@ public static void LogWarning(string message, params object[] args)
/// <param name="args">An object array that contains zero or more objects to format.</param>
private static T LogExceptionImpl<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(EventLevel eventLevel, string argumentName, Exception innerException, string format, params object[] args) where T : Exception
{
string message = null;

string message;
if (args != null)
message = string.Format(CultureInfo.InvariantCulture, format, args);
else
Expand All @@ -329,9 +326,9 @@ public static void LogWarning(string message, params object[] args)
if (IdentityModelEventSource.Logger.IsEnabled() && IdentityModelEventSource.Logger.LogLevel >= eventLevel)
IdentityModelEventSource.Logger.Write(eventLevel, innerException, message);

EventLogLevel eventLogLevel = Enum.IsDefined(typeof(EventLogLevel), (int)eventLevel) ? (EventLogLevel)eventLevel : EventLogLevel.Error;
EventLogLevel eventLogLevel = EventLevelToEventLogLevel(eventLevel);
if (Logger.IsEnabled(eventLogLevel))
Logger.Log(WriteEntry((EventLogLevel)eventLevel, innerException, message, null));
Logger.Log(WriteEntry(eventLogLevel, innerException, message, null));

if (innerException != null)
if (string.IsNullOrEmpty(argumentName))
Expand All @@ -345,6 +342,9 @@ public static void LogWarning(string message, params object[] args)
return (T)Activator.CreateInstance(typeof(T), argumentName, message);
}

private static EventLogLevel EventLevelToEventLogLevel(EventLevel eventLevel) =>
(uint)(int)eventLevel <= 5 ? (EventLogLevel)eventLevel : EventLogLevel.Error;

/// <summary>
/// Formats the string using InvariantCulture
/// </summary>
Expand Down

0 comments on commit 4cdb715

Please sign in to comment.