From ca9bbc4de8843b4604a43f87166e69f1ad83d9dc Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Mon, 21 Jun 2021 20:31:00 -0700 Subject: [PATCH 1/3] Change preview overloads, take LogDefineOptions Related to #50913 --- .../gen/LoggerMessageGenerator.Emitter.cs | 2 +- ...crosoft.Extensions.Logging.Abstractions.cs | 19 ++++--- .../src/LogDefineOptions.cs | 18 ++++++ .../src/LoggerMessage.cs | 56 +++++++++---------- .../TestWithNestedClass.generated.txt | 2 +- .../TestWithSkipEnabledCheck.generated.txt | 2 +- .../Baselines/TestWithTwoParams.generated.txt | 2 +- 7 files changed, 62 insertions(+), 39 deletions(-) create mode 100644 src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LogDefineOptions.cs diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Emitter.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Emitter.cs index 908efaf6afe325..16c16f482aadd8 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Emitter.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Emitter.cs @@ -370,7 +370,7 @@ private void GenLogMethod(LoggerMethod lm, string nestedIndentation) GenDefineTypes(lm, brackets: true); - _builder.Append(@$"({level}, new global::Microsoft.Extensions.Logging.EventId({lm.EventId}, {eventName}), ""{ConvertEndOfLineAndQuotationCharactersToEscapeForm(lm.Message)}"", true); + _builder.Append(@$"({level}, new global::Microsoft.Extensions.Logging.EventId({lm.EventId}, {eventName}), ""{ConvertEndOfLineAndQuotationCharactersToEscapeForm(lm.Message)}"", new global::Microsoft.Extensions.Logging.LogDefineOptions() {{ SkipEnabledCheck = true }}); "); } 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 ef9d46564ec61c..d964773de48aa4 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 @@ -48,6 +48,11 @@ public partial interface ISupportExternalScope { void SetScopeProvider(Microsoft.Extensions.Logging.IExternalScopeProvider scopeProvider); } + public partial class LogDefineOptions + { + public LogDefineOptions() { } + public bool SkipEnabledCheck { get { throw null; } set { } } + } public static partial class LoggerExtensions { public static System.IDisposable BeginScope(this Microsoft.Extensions.Logging.ILogger logger, string messageFormat, params object?[] args) { throw null; } @@ -94,7 +99,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.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString, Microsoft.Extensions.Logging.LogDefineOptions options) { 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; } @@ -103,17 +108,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, Microsoft.Extensions.Logging.LogDefineOptions options) { 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, Microsoft.Extensions.Logging.LogDefineOptions options) { 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, Microsoft.Extensions.Logging.LogDefineOptions options) { 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, Microsoft.Extensions.Logging.LogDefineOptions options) { 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, Microsoft.Extensions.Logging.LogDefineOptions options) { 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, Microsoft.Extensions.Logging.LogDefineOptions options) { throw null; } } [System.AttributeUsageAttribute(System.AttributeTargets.Method)] public sealed partial class LoggerMessageAttribute : System.Attribute diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LogDefineOptions.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LogDefineOptions.cs new file mode 100644 index 00000000000000..22e036093c60b0 --- /dev/null +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LogDefineOptions.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; + +namespace Microsoft.Extensions.Logging +{ + /// + /// Options for and its overloads + /// + public class LogDefineOptions + { + /// + /// Gets the flag to skip IsEnabled check for the logging method. + /// + public bool SkipEnabledCheck { get; set; } + } +} diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs index ee40fc116bc38e..b2e1393f7c7916 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs @@ -128,7 +128,7 @@ 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); + => Define(logLevel, eventId, formatString, options: new LogDefineOptions() { SkipEnabledCheck = false}); /// /// Creates a delegate which can be invoked for logging a message. @@ -136,9 +136,9 @@ public static Func DefineScopeThe /// The event id /// The named format string - /// Skips the check if the logging category is enabled. + /// The /// A delegate which when invoked creates a log message. - public static Action Define(LogLevel logLevel, EventId eventId, string formatString, bool skipEnabledCheck) + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 0); @@ -147,7 +147,7 @@ void Log(ILogger logger, Exception? exception) logger.Log(logLevel, eventId, new LogValues(formatter), exception, LogValues.Callback); } - if (skipEnabledCheck) + if (options.SkipEnabledCheck) { return Log; } @@ -170,7 +170,7 @@ void Log(ILogger logger, 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); + => Define(logLevel, eventId, formatString, options: new LogDefineOptions() { SkipEnabledCheck = false}); /// /// Creates a delegate which can be invoked for logging a message. @@ -179,9 +179,9 @@ void Log(ILogger logger, Exception? exception) /// The /// The event id /// The named format string - /// Skips the check if the logging category is enabled. + /// The /// A delegate which when invoked creates a log message. - public static Action Define(LogLevel logLevel, EventId eventId, string formatString, bool skipEnabledCheck) + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 1); @@ -190,7 +190,7 @@ void Log(ILogger logger, T1 arg1, Exception? exception) logger.Log(logLevel, eventId, new LogValues(formatter, arg1), exception, LogValues.Callback); } - if (skipEnabledCheck) + if (options.SkipEnabledCheck) { return Log; } @@ -214,7 +214,7 @@ 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); + => Define(logLevel, eventId, formatString, options: new LogDefineOptions() { SkipEnabledCheck = false}); /// /// Creates a delegate which can be invoked for logging a message. @@ -224,9 +224,9 @@ void Log(ILogger logger, T1 arg1, Exception? exception) /// The /// The event id /// The named format string - /// Skips the check if the logging category is enabled. + /// The /// A delegate which when invoked creates a log message. - public static Action Define(LogLevel logLevel, EventId eventId, string formatString, bool skipEnabledCheck) + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 2); @@ -235,7 +235,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, Exception? exception) logger.Log(logLevel, eventId, new LogValues(formatter, arg1, arg2), exception, LogValues.Callback); } - if (skipEnabledCheck) + if (options.SkipEnabledCheck) { return Log; } @@ -260,7 +260,7 @@ 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); + => Define(logLevel, eventId, formatString, options: new LogDefineOptions() { SkipEnabledCheck = false}); /// /// Creates a delegate which can be invoked for logging a message. @@ -271,9 +271,9 @@ void Log(ILogger logger, T1 arg1, T2 arg2, Exception? exception) /// The /// The event id /// The named format string - /// Skips the check if the logging category is enabled. + /// The /// A delegate which when invoked creates a log message. - public static Action Define(LogLevel logLevel, EventId eventId, string formatString, bool skipEnabledCheck) + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 3); @@ -282,7 +282,7 @@ 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) + if (options.SkipEnabledCheck) { return Log; } @@ -308,7 +308,7 @@ 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); + => Define(logLevel, eventId, formatString, options: new LogDefineOptions() { SkipEnabledCheck = false}); /// /// Creates a delegate which can be invoked for logging a message. @@ -320,9 +320,9 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, Exception? exception) /// The /// The event id /// The named format string - /// Skips the check if the logging category is enabled. + /// The /// A delegate which when invoked creates a log message. - public static Action Define(LogLevel logLevel, EventId eventId, string formatString, bool skipEnabledCheck) + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 4); @@ -331,7 +331,7 @@ 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) + if (options.SkipEnabledCheck) { return Log; } @@ -358,7 +358,7 @@ 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); + => Define(logLevel, eventId, formatString, options: new LogDefineOptions() { SkipEnabledCheck = false}); /// /// Creates a delegate which can be invoked for logging a message. @@ -371,9 +371,9 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, Exception? exceptio /// The /// The event id /// The named format string - /// Skips the check if the logging category is enabled. + /// The /// A delegate which when invoked creates a log message. - public static Action Define(LogLevel logLevel, EventId eventId, string formatString, bool skipEnabledCheck) + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 5); @@ -382,7 +382,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, Exception? logger.Log(logLevel, eventId, new LogValues(formatter, arg1, arg2, arg3, arg4, arg5), exception, LogValues.Callback); } - if (skipEnabledCheck) + if (options.SkipEnabledCheck) { return Log; } @@ -410,7 +410,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, 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); + => Define(logLevel, eventId, formatString, options: new LogDefineOptions() { SkipEnabledCheck = false}); /// /// Creates a delegate which can be invoked for logging a message. @@ -424,9 +424,9 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, Exception? /// The /// The event id /// The named format string - /// Skips the check if the logging category is enabled. + /// The /// A delegate which when invoked creates a log message. - public static Action Define(LogLevel logLevel, EventId eventId, string formatString, bool skipEnabledCheck) + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 6); @@ -435,7 +435,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, E logger.Log(logLevel, eventId, new LogValues(formatter, arg1, arg2, arg3, arg4, arg5, arg6), exception, LogValues.Callback); } - if (skipEnabledCheck) + if (options.SkipEnabledCheck) { return Log; } diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/Baselines/TestWithNestedClass.generated.txt b/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/Baselines/TestWithNestedClass.generated.txt index 024e0464c715f8..e10a678aef24cf 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/Baselines/TestWithNestedClass.generated.txt +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/Baselines/TestWithNestedClass.generated.txt @@ -17,7 +17,7 @@ namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses.NestedNamesp { [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "6.0.0.0")] private static readonly global::System.Action __M9Callback = - global::Microsoft.Extensions.Logging.LoggerMessage.Define(global::Microsoft.Extensions.Logging.LogLevel.Debug, new global::Microsoft.Extensions.Logging.EventId(9, nameof(M9)), "M9", true); + global::Microsoft.Extensions.Logging.LoggerMessage.Define(global::Microsoft.Extensions.Logging.LogLevel.Debug, new global::Microsoft.Extensions.Logging.EventId(9, nameof(M9)), "M9", new global::Microsoft.Extensions.Logging.LogDefineOptions() { SkipEnabledCheck = true }); [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "6.0.0.0")] public static partial void M9(global::Microsoft.Extensions.Logging.ILogger logger) diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/Baselines/TestWithSkipEnabledCheck.generated.txt b/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/Baselines/TestWithSkipEnabledCheck.generated.txt index c4b242a0a9e17d..6576bb4b394f1b 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/Baselines/TestWithSkipEnabledCheck.generated.txt +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/Baselines/TestWithSkipEnabledCheck.generated.txt @@ -7,7 +7,7 @@ namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses { [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "6.0.0.0")] private static readonly global::System.Action __M0Callback = - global::Microsoft.Extensions.Logging.LoggerMessage.Define(global::Microsoft.Extensions.Logging.LogLevel.Information, new global::Microsoft.Extensions.Logging.EventId(0, nameof(M0)), "Message: When using SkipEnabledCheck, the generated code skips logger.IsEnabled(logLevel) check before calling log. To be used when consumer has already guarded logger method in an IsEnabled check.", true); + global::Microsoft.Extensions.Logging.LoggerMessage.Define(global::Microsoft.Extensions.Logging.LogLevel.Information, new global::Microsoft.Extensions.Logging.EventId(0, nameof(M0)), "Message: When using SkipEnabledCheck, the generated code skips logger.IsEnabled(logLevel) check before calling log. To be used when consumer has already guarded logger method in an IsEnabled check.", new global::Microsoft.Extensions.Logging.LogDefineOptions() { SkipEnabledCheck = true }); [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "6.0.0.0")] public static partial void M0(global::Microsoft.Extensions.Logging.ILogger logger) diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/Baselines/TestWithTwoParams.generated.txt b/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/Baselines/TestWithTwoParams.generated.txt index ae5b89b74a4d47..5daee3ddd43a1e 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/Baselines/TestWithTwoParams.generated.txt +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/Baselines/TestWithTwoParams.generated.txt @@ -7,7 +7,7 @@ namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses { [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "6.0.0.0")] private static readonly global::System.Action, global::System.Exception?> __M0Callback = - global::Microsoft.Extensions.Logging.LoggerMessage.Define>(global::Microsoft.Extensions.Logging.LogLevel.Error, new global::Microsoft.Extensions.Logging.EventId(0, nameof(M0)), "M0 {a1} {a2}", true); + global::Microsoft.Extensions.Logging.LoggerMessage.Define>(global::Microsoft.Extensions.Logging.LogLevel.Error, new global::Microsoft.Extensions.Logging.EventId(0, nameof(M0)), "M0 {a1} {a2}", new global::Microsoft.Extensions.Logging.LogDefineOptions() { SkipEnabledCheck = true }); [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "6.0.0.0")] public static partial void M0(global::Microsoft.Extensions.Logging.ILogger logger, global::System.Int32 a1, global::System.Collections.Generic.IEnumerable a2) From b53056173b072296115b373a96c900e39d25c409 Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Thu, 24 Jun 2021 09:08:05 -0700 Subject: [PATCH 2/3] Apply PR feedback --- ...crosoft.Extensions.Logging.Abstractions.cs | 14 +++---- .../src/LogDefineOptions.cs | 2 +- .../src/LoggerMessage.cs | 42 +++++++++---------- 3 files changed, 29 insertions(+), 29 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 d964773de48aa4..1404880a972882 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 @@ -99,7 +99,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, Microsoft.Extensions.Logging.LogDefineOptions options) { throw null; } + public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString, Microsoft.Extensions.Logging.LogDefineOptions? options) { 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; } @@ -108,17 +108,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, Microsoft.Extensions.Logging.LogDefineOptions options) { throw null; } + public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString, Microsoft.Extensions.Logging.LogDefineOptions? options) { 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, Microsoft.Extensions.Logging.LogDefineOptions options) { throw null; } + public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString, Microsoft.Extensions.Logging.LogDefineOptions? options) { 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, Microsoft.Extensions.Logging.LogDefineOptions options) { throw null; } + public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString, Microsoft.Extensions.Logging.LogDefineOptions? options) { 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, Microsoft.Extensions.Logging.LogDefineOptions options) { throw null; } + public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString, Microsoft.Extensions.Logging.LogDefineOptions? options) { 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, Microsoft.Extensions.Logging.LogDefineOptions options) { throw null; } + public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString, Microsoft.Extensions.Logging.LogDefineOptions? options) { 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, Microsoft.Extensions.Logging.LogDefineOptions options) { throw null; } + public static System.Action Define(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, string formatString, Microsoft.Extensions.Logging.LogDefineOptions? options) { throw null; } } [System.AttributeUsageAttribute(System.AttributeTargets.Method)] public sealed partial class LoggerMessageAttribute : System.Attribute diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LogDefineOptions.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LogDefineOptions.cs index 22e036093c60b0..48016fcd2b7814 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LogDefineOptions.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LogDefineOptions.cs @@ -11,7 +11,7 @@ namespace Microsoft.Extensions.Logging public class LogDefineOptions { /// - /// Gets the flag to skip IsEnabled check for the logging method. + /// Gets or sets the flag to skip IsEnabled check for the logging method. /// public bool SkipEnabledCheck { get; set; } } diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs index b2e1393f7c7916..db1ffdfd1d89f9 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/src/LoggerMessage.cs @@ -128,7 +128,7 @@ 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, options: new LogDefineOptions() { SkipEnabledCheck = false}); + => Define(logLevel, eventId, formatString, options: null); /// /// Creates a delegate which can be invoked for logging a message. @@ -138,7 +138,7 @@ public static Func DefineScopeThe named format string /// The /// A delegate which when invoked creates a log message. - public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions options) + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions? options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 0); @@ -147,7 +147,7 @@ void Log(ILogger logger, Exception? exception) logger.Log(logLevel, eventId, new LogValues(formatter), exception, LogValues.Callback); } - if (options.SkipEnabledCheck) + if (options != null && options.SkipEnabledCheck) { return Log; } @@ -170,7 +170,7 @@ void Log(ILogger logger, 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, options: new LogDefineOptions() { SkipEnabledCheck = false}); + => Define(logLevel, eventId, formatString, options: null); /// /// Creates a delegate which can be invoked for logging a message. @@ -181,7 +181,7 @@ void Log(ILogger logger, Exception? exception) /// The named format string /// The /// A delegate which when invoked creates a log message. - public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions options) + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions? options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 1); @@ -190,7 +190,7 @@ void Log(ILogger logger, T1 arg1, Exception? exception) logger.Log(logLevel, eventId, new LogValues(formatter, arg1), exception, LogValues.Callback); } - if (options.SkipEnabledCheck) + if (options != null && options.SkipEnabledCheck) { return Log; } @@ -214,7 +214,7 @@ 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, options: new LogDefineOptions() { SkipEnabledCheck = false}); + => Define(logLevel, eventId, formatString, options: null); /// /// Creates a delegate which can be invoked for logging a message. @@ -226,7 +226,7 @@ void Log(ILogger logger, T1 arg1, Exception? exception) /// The named format string /// The /// A delegate which when invoked creates a log message. - public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions options) + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions? options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 2); @@ -235,7 +235,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, Exception? exception) logger.Log(logLevel, eventId, new LogValues(formatter, arg1, arg2), exception, LogValues.Callback); } - if (options.SkipEnabledCheck) + if (options != null && options.SkipEnabledCheck) { return Log; } @@ -260,7 +260,7 @@ 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, options: new LogDefineOptions() { SkipEnabledCheck = false}); + => Define(logLevel, eventId, formatString, options: null); /// /// Creates a delegate which can be invoked for logging a message. @@ -273,7 +273,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, Exception? exception) /// The named format string /// The /// A delegate which when invoked creates a log message. - public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions options) + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions? options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 3); @@ -282,7 +282,7 @@ 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 (options.SkipEnabledCheck) + if (options != null && options.SkipEnabledCheck) { return Log; } @@ -308,7 +308,7 @@ 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, options: new LogDefineOptions() { SkipEnabledCheck = false}); + => Define(logLevel, eventId, formatString, options: null); /// /// Creates a delegate which can be invoked for logging a message. @@ -322,7 +322,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, Exception? exception) /// The named format string /// The /// A delegate which when invoked creates a log message. - public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions options) + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions? options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 4); @@ -331,7 +331,7 @@ 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 (options.SkipEnabledCheck) + if (options != null && options.SkipEnabledCheck) { return Log; } @@ -358,7 +358,7 @@ 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, options: new LogDefineOptions() { SkipEnabledCheck = false}); + => Define(logLevel, eventId, formatString, options: null); /// /// Creates a delegate which can be invoked for logging a message. @@ -373,7 +373,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, Exception? exceptio /// The named format string /// The /// A delegate which when invoked creates a log message. - public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions options) + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions? options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 5); @@ -382,7 +382,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, Exception? logger.Log(logLevel, eventId, new LogValues(formatter, arg1, arg2, arg3, arg4, arg5), exception, LogValues.Callback); } - if (options.SkipEnabledCheck) + if (options != null && options.SkipEnabledCheck) { return Log; } @@ -410,7 +410,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, 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, options: new LogDefineOptions() { SkipEnabledCheck = false}); + => Define(logLevel, eventId, formatString, options: null); /// /// Creates a delegate which can be invoked for logging a message. @@ -426,7 +426,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, Exception? /// The named format string /// The /// A delegate which when invoked creates a log message. - public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions options) + public static Action Define(LogLevel logLevel, EventId eventId, string formatString, LogDefineOptions? options) { LogValuesFormatter formatter = CreateLogValuesFormatter(formatString, expectedNamedParameterCount: 6); @@ -435,7 +435,7 @@ void Log(ILogger logger, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, E logger.Log(logLevel, eventId, new LogValues(formatter, arg1, arg2, arg3, arg4, arg5, arg6), exception, LogValues.Callback); } - if (options.SkipEnabledCheck) + if (options != null && options.SkipEnabledCheck) { return Log; } From 040350baeef88383f8dc39c580aebc99aaa52304 Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Fri, 25 Jun 2021 11:28:13 -0700 Subject: [PATCH 3/3] Fix compile issue --- .../tests/Common/LoggerMessageTest.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging/tests/Common/LoggerMessageTest.cs b/src/libraries/Microsoft.Extensions.Logging/tests/Common/LoggerMessageTest.cs index 6422a7a188b86c..a47ccc154fc6c2 100644 --- a/src/libraries/Microsoft.Extensions.Logging/tests/Common/LoggerMessageTest.cs +++ b/src/libraries/Microsoft.Extensions.Logging/tests/Common/LoggerMessageTest.cs @@ -430,13 +430,13 @@ public void DefineAndDefineScope_ThrowsException_WhenFormatString_IsNull(Delegat public static IEnumerable LogMessagesDataSkipEnabledCheck => new[] { - new object[] { LoggerMessage.Define(LogLevel.Error, 0, "Log ", skipEnabledCheck: true), 0 }, - new object[] { LoggerMessage.Define(LogLevel.Error, 1, "Log {P0}", skipEnabledCheck: true), 1 }, - new object[] { LoggerMessage.Define(LogLevel.Error, 2, "Log {P0} {P1}", skipEnabledCheck: true), 2 }, - new object[] { LoggerMessage.Define(LogLevel.Error, 3, "Log {P0} {P1} {P2}", skipEnabledCheck: true), 3 }, - new object[] { LoggerMessage.Define(LogLevel.Error, 4, "Log {P0} {P1} {P2} {P3}", skipEnabledCheck: true), 4 }, - new object[] { LoggerMessage.Define(LogLevel.Error, 5, "Log {P0} {P1} {P2} {P3} {P4}", skipEnabledCheck: true), 5 }, - new object[] { LoggerMessage.Define(LogLevel.Error, 6, "Log {P0} {P1} {P2} {P3} {P4} {P5}", skipEnabledCheck: true), 6 }, + new object[] { LoggerMessage.Define(LogLevel.Error, 0, "Log ", options: new LogDefineOptions() { SkipEnabledCheck = true }), 0 }, + new object[] { LoggerMessage.Define(LogLevel.Error, 1, "Log {P0}", options: new LogDefineOptions() { SkipEnabledCheck = true }), 1 }, + new object[] { LoggerMessage.Define(LogLevel.Error, 2, "Log {P0} {P1}", options: new LogDefineOptions() { SkipEnabledCheck = true }), 2 }, + new object[] { LoggerMessage.Define(LogLevel.Error, 3, "Log {P0} {P1} {P2}", options: new LogDefineOptions() { SkipEnabledCheck = true }), 3 }, + new object[] { LoggerMessage.Define(LogLevel.Error, 4, "Log {P0} {P1} {P2} {P3}", options: new LogDefineOptions() { SkipEnabledCheck = true }), 4 }, + new object[] { LoggerMessage.Define(LogLevel.Error, 5, "Log {P0} {P1} {P2} {P3} {P4}", options: new LogDefineOptions() { SkipEnabledCheck = true }), 5 }, + new object[] { LoggerMessage.Define(LogLevel.Error, 6, "Log {P0} {P1} {P2} {P3} {P4} {P5}", options: new LogDefineOptions() { SkipEnabledCheck = true }), 6 }, }; private delegate Delegate Define(LogLevel logLevel, EventId eventId, string formatString);