From c0cc7847793777e0aeaa92350ae8e43fd9b43c92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Sat, 16 Jan 2021 12:42:38 +0100 Subject: [PATCH 1/4] LoggerMessage.Define with overload to disable IsEnabled check --- ...crosoft.Extensions.Logging.Abstractions.cs | 6 + .../src/LoggerMessage.cs | 112 ++++++++++++++++-- 2 files changed, 111 insertions(+), 7 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/ref/Microsoft.Extensions.Logging.Abstractions.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/ref/Microsoft.Extensions.Logging.Abstractions.cs index e2d403bc7b38cf..ed39268cbd3b61 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/ref/Microsoft.Extensions.Logging.Abstractions.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/ref/Microsoft.Extensions.Logging.Abstractions.cs @@ -102,11 +102,17 @@ public static partial class LoggerMessage public static System.Func DefineScope(string formatString) { throw null; } public static System.Func DefineScope(string formatString) { throw null; } public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString) { throw null; } + public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString, bool skipEnabledCheck) { throw null; } public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString) { throw null; } + public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString, bool skipEnabledCheck) { throw null; } public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString) { throw null; } + public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString, bool skipEnabledCheck) { throw null; } public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString) { throw null; } + public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString, bool skipEnabledCheck) { throw null; } public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString) { throw null; } + public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString, bool skipEnabledCheck) { throw null; } public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString) { throw null; } + public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString, bool skipEnabledCheck) { throw null; } } public partial class Logger : Microsoft.Extensions.Logging.ILogger, Microsoft.Extensions.Logging.ILogger { diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs index 7007782a7a2a20..438b41c439989c 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs @@ -128,12 +128,23 @@ public static Func DefineScopeThe named format string /// A delegate which when invoked creates a log message. public static Action Define(LogLevel logLevel, EventId eventId, string formatString) + => Define(logLevel, eventId, formatString, skipEnabledCheck: false); + + /// + /// Creates a delegate which can be invoked for logging a message. + /// + /// The + /// The event id + /// The named format string + /// Skips the check if the logging category is enabled. + /// A delegate which when invoked creates a log message. + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, bool skipEnabledCheck) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 0); return (logger, exception) => { - if (logger.IsEnabled(logLevel)) + if (skipEnabledCheck || logger.IsEnabled(logLevel)) { logger.Log(logLevel, eventId, new LogValues(formatter), exception, LogValues.Callback); } @@ -149,6 +160,18 @@ public static Func DefineScopeThe named format string /// A delegate which when invoked creates a log message. public static Action Define(LogLevel logLevel, EventId eventId, string formatString) + => Define(logLevel, eventId, formatString, skipEnabledCheck: false); + + /// + /// Creates a delegate which can be invoked for logging a message. + /// + /// The type of the first parameter passed to the named format string. + /// The + /// The event id + /// The named format string + /// Skips the check if the logging category is enabled. + /// A delegate which when invoked creates a log message. + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, bool skipEnabledCheck) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 1); @@ -159,7 +182,7 @@ void Log(ILogger logger, T1 arg1, Exception? exception) return (logger, arg1, exception) => { - if (logger.IsEnabled(logLevel)) + if (skipEnabledCheck || logger.IsEnabled(logLevel)) { Log(logger, arg1, exception); } @@ -176,6 +199,19 @@ void Log(ILogger logger, T1 arg1, Exception? exception) /// The named format string /// A delegate which when invoked creates a log message. public static Action Define(LogLevel logLevel, EventId eventId, string formatString) + => Define(logLevel, eventId, formatString, skipEnabledCheck: false); + + /// + /// Creates a delegate which can be invoked for logging a message. + /// + /// The type of the first parameter passed to the named format string. + /// The type of the second parameter passed to the named format string. + /// The + /// The event id + /// The named format string + /// Skips the check if the logging category is enabled. + /// A delegate which when invoked creates a log message. + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, bool skipEnabledCheck) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 2); @@ -186,7 +222,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, Exception? exception) return (logger, arg1, arg2, exception) => { - if (logger.IsEnabled(logLevel)) + if (skipEnabledCheck || logger.IsEnabled(logLevel)) { Log(logger, arg1, arg2, exception); } @@ -204,6 +240,20 @@ void Log(ILogger logger, T1 arg1, T2 arg2, Exception? exception) /// The named format string /// A delegate which when invoked creates a log message. public static Action Define(LogLevel logLevel, EventId eventId, string formatString) + => Define(logLevel, eventId, formatString, skipEnabledCheck: false); + + /// + /// Creates a delegate which can be invoked for logging a message. + /// + /// The type of the first parameter passed to the named format string. + /// The type of the second parameter passed to the named format string. + /// The type of the third parameter passed to the named format string. + /// The + /// The event id + /// The named format string + /// Skips the check if the logging category is enabled. + /// A delegate which when invoked creates a log message. + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, bool skipEnabledCheck) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 3); @@ -214,7 +264,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, Exception? exception) return (logger, arg1, arg2, arg3, exception) => { - if (logger.IsEnabled(logLevel)) + if (skipEnabledCheck || logger.IsEnabled(logLevel)) { Log(logger, arg1, arg2, arg3, exception); } @@ -233,6 +283,21 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, Exception? exception) /// The named format string /// A delegate which when invoked creates a log message. public static Action Define(LogLevel logLevel, EventId eventId, string formatString) + => Define(logLevel, eventId, formatString, skipEnabledCheck: false); + + /// + /// Creates a delegate which can be invoked for logging a message. + /// + /// The type of the first parameter passed to the named format string. + /// The type of the second parameter passed to the named format string. + /// The type of the third parameter passed to the named format string. + /// The type of the fourth parameter passed to the named format string. + /// The + /// The event id + /// The named format string + /// Skips the check if the logging category is enabled. + /// A delegate which when invoked creates a log message. + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, bool skipEnabledCheck) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 4); @@ -243,7 +308,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, Exception? exceptio return (logger, arg1, arg2, arg3, arg4, exception) => { - if (logger.IsEnabled(logLevel)) + if (skipEnabledCheck || logger.IsEnabled(logLevel)) { Log(logger, arg1, arg2, arg3, arg4, exception); } @@ -263,12 +328,28 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, Exception? exceptio /// The named format string /// A delegate which when invoked creates a log message. public static Action Define(LogLevel logLevel, EventId eventId, string formatString) + => Define(logLevel, eventId, formatString, skipEnabledCheck: false); + + /// + /// Creates a delegate which can be invoked for logging a message. + /// + /// The type of the first parameter passed to the named format string. + /// The type of the second parameter passed to the named format string. + /// The type of the third parameter passed to the named format string. + /// The type of the fourth parameter passed to the named format string. + /// The type of the fifth parameter passed to the named format string. + /// The + /// The event id + /// The named format string + /// Skips the check if the logging category is enabled. + /// A delegate which when invoked creates a log message. + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, bool skipEnabledCheck) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 5); return (logger, arg1, arg2, arg3, arg4, arg5, exception) => { - if (logger.IsEnabled(logLevel)) + if (skipEnabledCheck || logger.IsEnabled(logLevel)) { logger.Log(logLevel, eventId, new LogValues(formatter, arg1, arg2, arg3, arg4, arg5), exception, LogValues.Callback); } @@ -289,12 +370,29 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, Exception? exceptio /// The named format string /// A delegate which when invoked creates a log message. public static Action Define(LogLevel logLevel, EventId eventId, string formatString) + => Define(logLevel, eventId, formatString, skipEnabledCheck: false); + + /// + /// Creates a delegate which can be invoked for logging a message. + /// + /// The type of the first parameter passed to the named format string. + /// The type of the second parameter passed to the named format string. + /// The type of the third parameter passed to the named format string. + /// The type of the fourth parameter passed to the named format string. + /// The type of the fifth parameter passed to the named format string. + /// The type of the sixth parameter passed to the named format string. + /// The + /// The event id + /// The named format string + /// Skips the check if the logging category is enabled. + /// A delegate which when invoked creates a log message. + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, bool skipEnabledCheck) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 6); return (logger, arg1, arg2, arg3, arg4, arg5, arg6, exception) => { - if (logger.IsEnabled(logLevel)) + if (skipEnabledCheck || logger.IsEnabled(logLevel)) { logger.Log(logLevel, eventId, new LogValues(formatter, arg1, arg2, arg3, arg4, arg5, arg6), exception, LogValues.Callback); } From 8979340f919dac12854fab69a9fb3ed69fc54239 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Sun, 28 Mar 2021 15:18:35 +0200 Subject: [PATCH 2/4] Hoist skipEnabledCheck --- .../src/LoggerMessage.cs | 70 ++++++++++++++++--- 1 file changed, 60 insertions(+), 10 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs index 438b41c439989c..f890b1a82ad575 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs @@ -142,11 +142,21 @@ public static Func DefineScope Log(logger, exception); + } + return (logger, exception) => { - if (skipEnabledCheck || logger.IsEnabled(logLevel)) + if (logger.IsEnabled(logLevel)) { - logger.Log(logLevel, eventId, new LogValues(formatter), exception, LogValues.Callback); + Log(logger, exception); } }; } @@ -180,9 +190,14 @@ void Log(ILogger logger, T1 arg1, Exception? exception) logger.Log(logLevel, eventId, new LogValues(formatter, arg1), exception, LogValues.Callback); } + if (skipEnabledCheck) + { + return (logger, arg1, exception) => Log(logger, arg1, exception); + } + return (logger, arg1, exception) => { - if (skipEnabledCheck || logger.IsEnabled(logLevel)) + if (logger.IsEnabled(logLevel)) { Log(logger, arg1, exception); } @@ -220,9 +235,14 @@ void Log(ILogger logger, T1 arg1, T2 arg2, Exception? exception) logger.Log(logLevel, eventId, new LogValues(formatter, arg1, arg2), exception, LogValues.Callback); } + if (skipEnabledCheck) + { + return (logger, arg1, arg2, exception) => Log(logger, arg1, arg2, exception); + } + return (logger, arg1, arg2, exception) => { - if (skipEnabledCheck || logger.IsEnabled(logLevel)) + if (logger.IsEnabled(logLevel)) { Log(logger, arg1, arg2, exception); } @@ -262,9 +282,14 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, Exception? exception) logger.Log(logLevel, eventId, new LogValues(formatter, arg1, arg2, arg3), exception, LogValues.Callback); } + if (skipEnabledCheck) + { + return (logger, arg1, arg2, arg3, exception) => Log(logger, arg1, arg2, arg3, exception); + } + return (logger, arg1, arg2, arg3, exception) => { - if (skipEnabledCheck || logger.IsEnabled(logLevel)) + if (logger.IsEnabled(logLevel)) { Log(logger, arg1, arg2, arg3, exception); } @@ -306,9 +331,14 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, Exception? exceptio logger.Log(logLevel, eventId, new LogValues(formatter, arg1, arg2, arg3, arg4), exception, LogValues.Callback); } + if (skipEnabledCheck) + { + return (logger, arg1, arg2, arg3, arg4, exception) => Log(logger, arg1, arg2, arg3, arg4, exception); + } + return (logger, arg1, arg2, arg3, arg4, exception) => { - if (skipEnabledCheck || logger.IsEnabled(logLevel)) + if (logger.IsEnabled(logLevel)) { Log(logger, arg1, arg2, arg3, arg4, exception); } @@ -347,11 +377,21 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, Exception? exceptio { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 5); + void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, Exception? exception) + { + logger.Log(logLevel, eventId, new LogValues(formatter, arg1, arg2, arg3, arg4, arg5), exception, LogValues.Callback); + } + + if (skipEnabledCheck) + { + return (logger, arg1, arg2, arg3, arg4, arg5, exception) => Log(logger, arg1, arg2, arg3, arg4, arg5, exception); + } + return (logger, arg1, arg2, arg3, arg4, arg5, exception) => { - if (skipEnabledCheck || logger.IsEnabled(logLevel)) + if (logger.IsEnabled(logLevel)) { - logger.Log(logLevel, eventId, new LogValues(formatter, arg1, arg2, arg3, arg4, arg5), exception, LogValues.Callback); + Log(logger, arg1, arg2, arg3, arg4, arg5, exception); } }; } @@ -390,11 +430,21 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, Exception? exceptio { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 6); + void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, Exception? exception) + { + logger.Log(logLevel, eventId, new LogValues(formatter, arg1, arg2, arg3, arg4, arg5, arg6), exception, LogValues.Callback); + } + + if (skipEnabledCheck) + { + return (logger, arg1, arg2, arg3, arg4, arg5, arg6, exception) => Log(logger, arg1, arg2, arg3, arg4, arg5, arg6, exception); + } + return (logger, arg1, arg2, arg3, arg4, arg5, arg6, exception) => { - if (skipEnabledCheck || logger.IsEnabled(logLevel)) + if (logger.IsEnabled(logLevel)) { - logger.Log(logLevel, eventId, new LogValues(formatter, arg1, arg2, arg3, arg4, arg5, arg6), exception, LogValues.Callback); + Log(logger, arg1, arg2, arg3, arg4, arg5, arg6, exception); } }; } From 778858a237a5012600889a2e49e47019491ecc6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Sun, 28 Mar 2021 16:01:15 +0200 Subject: [PATCH 3/4] Fixed ref-assembly --- .../ref/Microsoft.Extensions.Logging.Abstractions.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/ref/Microsoft.Extensions.Logging.Abstractions.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/ref/Microsoft.Extensions.Logging.Abstractions.cs index ed39268cbd3b61..a1051db9b0189f 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/ref/Microsoft.Extensions.Logging.Abstractions.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/ref/Microsoft.Extensions.Logging.Abstractions.cs @@ -94,6 +94,7 @@ public static partial class LoggerFactoryExtensions public static partial class LoggerMessage { public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString) { throw null; } + public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString, bool skipEnabledCheck) { throw null; } public static System.Func DefineScope(string formatString) { throw null; } public static System.Func DefineScope(string formatString) { throw null; } public static System.Func DefineScope(string formatString) { throw null; } From 3e547f15c3a976f70c166ac36c7ad8b70d470469 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Mon, 29 Mar 2021 09:13:46 +0200 Subject: [PATCH 4/4] Return the Log method for the skipEnabledCheck-branch directly So that it is (manually) inlined. Cf. https://github.com/dotnet/runtime/pull/50334#discussion_r602910766 --- .../src/LoggerMessage.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs index f890b1a82ad575..c10693ce3a0f30 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs @@ -149,7 +149,7 @@ void Log(ILogger logger, Exception? exception) if (skipEnabledCheck) { - return (logger, exception) => Log(logger, exception); + return Log; } return (logger, exception) => @@ -192,7 +192,7 @@ void Log(ILogger logger, T1 arg1, Exception? exception) if (skipEnabledCheck) { - return (logger, arg1, exception) => Log(logger, arg1, exception); + return Log; } return (logger, arg1, exception) => @@ -237,7 +237,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, Exception? exception) if (skipEnabledCheck) { - return (logger, arg1, arg2, exception) => Log(logger, arg1, arg2, exception); + return Log; } return (logger, arg1, arg2, exception) => @@ -284,7 +284,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, Exception? exception) if (skipEnabledCheck) { - return (logger, arg1, arg2, arg3, exception) => Log(logger, arg1, arg2, arg3, exception); + return Log; } return (logger, arg1, arg2, arg3, exception) => @@ -333,7 +333,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, Exception? exceptio if (skipEnabledCheck) { - return (logger, arg1, arg2, arg3, arg4, exception) => Log(logger, arg1, arg2, arg3, arg4, exception); + return Log; } return (logger, arg1, arg2, arg3, arg4, exception) => @@ -384,7 +384,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, Exception? if (skipEnabledCheck) { - return (logger, arg1, arg2, arg3, arg4, arg5, exception) => Log(logger, arg1, arg2, arg3, arg4, arg5, exception); + return Log; } return (logger, arg1, arg2, arg3, arg4, arg5, exception) => @@ -437,7 +437,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, E if (skipEnabledCheck) { - return (logger, arg1, arg2, arg3, arg4, arg5, arg6, exception) => Log(logger, arg1, arg2, arg3, arg4, arg5, arg6, exception); + return Log; } return (logger, arg1, arg2, arg3, arg4, arg5, arg6, exception) =>