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 e2d403bc7b38c..a1051db9b0189 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; } @@ -102,11 +103,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 7007782a7a2a2..c10693ce3a0f3 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs @@ -128,14 +128,35 @@ 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); + void Log(ILogger logger, Exception? exception) + { + logger.Log(logLevel, eventId, new LogValues(formatter), exception, LogValues.Callback); + } + + if (skipEnabledCheck) + { + return Log; + } + return (logger, exception) => { if (logger.IsEnabled(logLevel)) { - logger.Log(logLevel, eventId, new LogValues(formatter), exception, LogValues.Callback); + Log(logger, exception); } }; } @@ -149,6 +170,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); @@ -157,6 +190,11 @@ void Log(ILogger logger, T1 arg1, Exception? exception) logger.Log(logLevel, eventId, new LogValues(formatter, arg1), exception, LogValues.Callback); } + if (skipEnabledCheck) + { + return Log; + } + return (logger, arg1, exception) => { if (logger.IsEnabled(logLevel)) @@ -176,6 +214,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); @@ -184,6 +235,11 @@ 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 Log; + } + return (logger, arg1, arg2, exception) => { if (logger.IsEnabled(logLevel)) @@ -204,6 +260,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); @@ -212,6 +282,11 @@ 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 Log; + } + return (logger, arg1, arg2, arg3, exception) => { if (logger.IsEnabled(logLevel)) @@ -233,6 +308,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); @@ -241,6 +331,11 @@ 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 Log; + } + return (logger, arg1, arg2, arg3, arg4, exception) => { if (logger.IsEnabled(logLevel)) @@ -263,14 +358,40 @@ 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); + 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 Log; + } + return (logger, arg1, arg2, arg3, arg4, arg5, exception) => { 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); } }; } @@ -289,14 +410,41 @@ 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); + 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 Log; + } + return (logger, arg1, arg2, arg3, arg4, arg5, arg6, exception) => { 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); } }; }