From 8c7e6317feb0adccd43250921ae526030fe08a93 Mon Sep 17 00:00:00 2001 From: JakenVeina Date: Fri, 7 Aug 2020 20:50:33 -0500 Subject: [PATCH] Added additional LoggerMessage.DefineScope() overloads, with more type parameters (up to 6 like .Define()). Resolves https://github.com/dotnet/runtime/issues/35060 --- ...crosoft.Extensions.Logging.Abstractions.cs | 3 ++ .../src/LoggerMessage.cs | 51 +++++++++++++++++++ .../tests/Common/LoggerMessageTest.cs | 15 ++++++ 3 files changed, 69 insertions(+) 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 ce0feb995008bc..6c492ac9bd9cfc 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 @@ -98,6 +98,9 @@ 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.Func DefineScope(string formatString) { 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; } 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) { throw null; } public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString) { throw null; } diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs index dab0062fc60d8c..a039be3156f9e3 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs @@ -69,6 +69,57 @@ public static Func DefineScope(str return (logger, arg1, arg2, arg3) => logger.BeginScope(new LogValues(formatter, arg1, arg2, arg3)); } + /// + /// Creates a delegate which can be invoked to create a log scope. + /// + /// 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 named format string + /// A delegate which when invoked creates a log scope. + public static Func DefineScope(string formatString) + { + var formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 4); + + return (logger, arg1, arg2, arg3, arg4) => logger.BeginScope(new LogValues(formatter, arg1, arg2, arg3, arg4)); + } + + /// + /// Creates a delegate which can be invoked to create a log scope. + /// + /// 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 named format string + /// A delegate which when invoked creates a log scope. + public static Func DefineScope(string formatString) + { + var formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 5); + + return (logger, arg1, arg2, arg3, arg4, arg5) => logger.BeginScope(new LogValues(formatter, arg1, arg2, arg3, arg4, arg5)); + } + + /// + /// Creates a delegate which can be invoked to create a log scope. + /// + /// 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 sisxth parameter passed to the named format string. + /// The named format string + /// A delegate which when invoked creates a log scope. + public static Func DefineScope(string formatString) + { + var formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 6); + + return (logger, arg1, arg2, arg3, arg4, arg5, arg6) => logger.BeginScope(new LogValues(formatter, arg1, arg2, arg3, arg4, arg5, arg6)); + } + /// /// Creates a delegate which can be invoked for logging a message. /// diff --git a/src/libraries/Microsoft.Extensions.Logging/tests/Common/LoggerMessageTest.cs b/src/libraries/Microsoft.Extensions.Logging/tests/Common/LoggerMessageTest.cs index 9b50f555b5e90f..41a351b6f1d091 100644 --- a/src/libraries/Microsoft.Extensions.Logging/tests/Common/LoggerMessageTest.cs +++ b/src/libraries/Microsoft.Extensions.Logging/tests/Common/LoggerMessageTest.cs @@ -276,6 +276,9 @@ public void DefineScope_WithNoParameters_ThrowsException_WhenFormatString_HasNam [InlineData(1)] [InlineData(2)] [InlineData(3)] + [InlineData(4)] + [InlineData(5)] + [InlineData(6)] public void DefineScope_ThrowsException_WhenExpectedFormatStringParameterCount_NotFound( int expectedNamedParameterCount) { @@ -300,6 +303,18 @@ public void DefineScope_ThrowsException_WhenExpectedFormatStringParameterCount_N exception = Assert.Throws( () => LoggerMessage.DefineScope(formatString)); break; + case 4: + exception = Assert.Throws( + () => LoggerMessage.DefineScope(formatString)); + break; + case 5: + exception = Assert.Throws( + () => LoggerMessage.DefineScope(formatString)); + break; + case 6: + exception = Assert.Throws( + () => LoggerMessage.DefineScope(formatString)); + break; default: throw new ArgumentException($"Invalid value for '{nameof(expectedNamedParameterCount)}'"); }