-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SkipEnabledCheck
on LoggerMessageAttribute
#54305
Changes from 6 commits
46af543
221f272
1fa1344
fe5181c
82271d1
58e1a75
73fd442
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,20 @@ public sealed class LoggerMessageAttribute : Attribute | |
/// </summary> | ||
public LoggerMessageAttribute() { } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="LoggerMessageAttribute"/> class | ||
/// which is used to guide the production of a strongly-typed logging method. | ||
/// </summary> | ||
/// <param name="eventId">The log event Id.</param> | ||
/// <param name="level">The log level.</param> | ||
/// <param name="message">Format string of the log message.</param> | ||
public LoggerMessageAttribute(int eventId, LogLevel level, string message) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason not to have EventName here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was the only ctor approved since we assumed it to be the most commonly used. If we think there is value in adding another ctor with EventName included (4 args) then we can take it to API review. |
||
{ | ||
EventId = eventId; | ||
Level = level; | ||
Message = message; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the logging event id for the logging method. | ||
/// </summary> | ||
|
@@ -59,5 +73,10 @@ public LoggerMessageAttribute() { } | |
/// Gets the message text for the logging method. | ||
/// </summary> | ||
public string Message { get; set; } = ""; | ||
|
||
/// <summary> | ||
/// Gets the flag to skip IsEnabled check for the logging method. | ||
/// </summary> | ||
public bool SkipEnabledCheck { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// <auto-generated/> | ||
#nullable enable | ||
|
||
namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses | ||
{ | ||
partial class TestWithDefaultValues | ||
{ | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "6.0.0.0")] | ||
private readonly struct __M0Struct : global::System.Collections.Generic.IReadOnlyList<global::System.Collections.Generic.KeyValuePair<string, object?>> | ||
{ | ||
|
||
public override string ToString() | ||
{ | ||
|
||
return $""; | ||
} | ||
|
||
public static string Format(__M0Struct state, global::System.Exception? ex) => state.ToString(); | ||
|
||
public int Count => 1; | ||
|
||
public global::System.Collections.Generic.KeyValuePair<string, object?> this[int index] | ||
{ | ||
get => index switch | ||
{ | ||
0 => new global::System.Collections.Generic.KeyValuePair<string, object?>("{OriginalFormat}", ""), | ||
|
||
_ => throw new global::System.IndexOutOfRangeException(nameof(index)), // return the same exception LoggerMessage.Define returns in this case | ||
}; | ||
} | ||
|
||
public global::System.Collections.Generic.IEnumerator<global::System.Collections.Generic.KeyValuePair<string, object?>> GetEnumerator() | ||
{ | ||
for (int i = 0; i < 1; i++) | ||
{ | ||
yield return this[i]; | ||
} | ||
} | ||
|
||
global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); | ||
} | ||
|
||
[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::Microsoft.Extensions.Logging.LogLevel level) | ||
{ | ||
if (logger.IsEnabled(level)) | ||
{ | ||
logger.Log( | ||
level, | ||
new global::Microsoft.Extensions.Logging.EventId(-1, nameof(M0)), | ||
new __M0Struct(), | ||
null, | ||
__M0Struct.Format); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// <auto-generated/> | ||
#nullable enable | ||
|
||
namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses | ||
{ | ||
partial class TestWithSkipEnabledCheck | ||
{ | ||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "6.0.0.0")] | ||
private static readonly global::System.Action<global::Microsoft.Extensions.Logging.ILogger, global::System.Exception?> __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::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "6.0.0.0")] | ||
public static partial void M0(global::Microsoft.Extensions.Logging.ILogger logger) | ||
{ | ||
__M0Callback(logger, null); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to use a tuple anymore? Can this just be regular variables?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no but I thought it would be neater this way, all defined in one line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not crazy about it... Especially because I need to match up the variable with the value on the right-hand side to see what its default value is.