-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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 additional LoggerMessageAttribute constructors #87254
Comments
Here's the set: public sealed class LoggerMessageAttribute : Attribute
{
// existing
public LoggerMessageAttribute()
public LoggerMessageAttribute(int eventId, LogLevel level, string message)
// proposed additions
public LoggerMessageAttribute(int eventId, LogLevel level)
public LoggerMessageAttribute(LogLevel level, string message)
public LoggerMessageAttribute(LogLevel level)
public LoggerMessageAttribute(string message)
public LoggerMessageAttribute(int eventId, string message)
public LoggerMessageAttribute(int eventId)
// existing
public int EventId { get; set; } = -1;
public string? EventName { get; set; }
public LogLevel Level { get; set; } = LogLevel.None;
public string Message { get; set; } = "";
public bool SkipEnabledCheck { get; set; }
} So:
Bottom line is that the lack of these contrusctors leads to considerably more ceremony for the customers. More stuff to type with no upside to them. |
Tagging subscribers to this area: @dotnet/area-extensions-logging Issue Detailsdotnet/extensions supports a variety of shortcut constructors on LogMethodAttribute. In the past these shortcuts were deemed undesirable. While I don't know the details behind the original decisions, now that we have more history of internal usage and a better track record we should reconsider. @geeknoid - can you enumerate exactly which APIs you hope to bring over?
|
If this is the case, why are we proposing the following constructors? would be better reducing a more helper constructors in such scenario. public LoggerMessageAttribute(int eventId, LogLevel level)
public LoggerMessageAttribute(int eventId, string message)
public LoggerMessageAttribute(int eventide) |
@tarekgh I hate it when people point out logical flaws in my stuff :-) Yeah, I suppose it would be reasonable to not include those. |
Just a heads up, I suspect without changes to static analysis users that attempt to use the new attribute constructors will get a warning that multiple events use the same -1 id, and warnings that the message string doesn't include their parameter names. Presumably we'd want to remove those warnings. |
@noahfalk what you are referring to is the source gen diagnostic SYSLIB1006 which we already changed it to be just runtime/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/DiagnosticDescriptors.cs Line 48 in 7de4e1b
We may consider changing the code to ignore the default event id runtime/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs Line 269 in 7de4e1b
If there is not any other concern, I'll modify the proposal and post it in the description, and we can proceed with the design review. |
namespace Microsoft.Extensions.Logging;
public sealed partial class LoggerMessageAttribute : Attribute
{
// Existing:
// public LoggerMessageAttribute();
// public LoggerMessageAttribute(int eventId, LogLevel level, string message);
public LoggerMessageAttribute(LogLevel level, string message);
public LoggerMessageAttribute(LogLevel level);
public LoggerMessageAttribute(string message);
} |
A few follow-ups:
internal static class Logging
{
[LoggerMessage(LogLevel.Information)]
void CreateRecord(this ILogger logger, string recordId);
[LoggerMessage(LogLevel.Debug)]
void DeleteRecord(this ILogger logger, string recordId);
[LoggerMessage("A record is being skipped: {recordId}")]
void SkipRecord(this ILogger logger, string recordId);
[LoggerMessage(LogLevel.Warning, "I am a broken record: {recordId}")]
void BrokenRecord(this ILogger logger, string recordId);
} As opposed to the more verbose: internal static class Logging
{
[LoggerMessage(LogLevel = LogLevel.Information)]
void CreateRecord(this ILogger logger, string recordId);
[LoggerMessage(LogLevel = LogLevel.Debug)]
void DeleteRecord(this ILogger logger, string recordId);
[LoggerMessage(Messgae = "A record is being skipped: {recordId}")]
void SkipRecord(this ILogger logger, string recordId);
[LoggerMessage(LogLevel = LogLevel.Warning, Message = "I am a broken record: {recordId}")]
void BrokenRecord(this ILogger logger, string recordId);
} There is simply no value in forcing the developer to enter this extra boilerplate throughout their code rather than adding a set of constructors that address the real-world mainstream use of the attribute. |
To add a little history, I think EventId got added to the original logging design to increase compat with ETW, and the id got added to the LoggerMessage attribute in turn because it was part of ILogger.Log(). I don't consider EventId existing to be a mistake, but I do agree with Martin that currently customers aren't getting much value from specifying it. Having it be optional feels better than having it be required. In the future some new scenarios like client-side EventId based filtering might provide customers more value around it, but other customers will continue having no reason to care about it. |
Marked as ready for review as @geeknoid want to discuss it one more time after he added more info to the issue. |
Given that we've already gone against guidelines by having the namespace Microsoft.Extensions.Logging;
public sealed partial class LoggerMessageAttribute : Attribute
{
// Existing:
// public LoggerMessageAttribute();
// public LoggerMessageAttribute(int eventId, LogLevel level, string message);
public LoggerMessageAttribute(LogLevel level, string message);
public LoggerMessageAttribute(LogLevel level);
public LoggerMessageAttribute(string message);
} |
By @geeknoid
Bottom line is that the lack of these constructors leads to considerably more ceremony for the customers. More stuff to type with no upside to them.
Proposal
Original description
dotnet/extensions supports a variety of shortcut constructors on LogMethodAttribute. In the past these shortcuts were deemed undesirable. While I don't know the details behind the original decisions, now that we have more history of internal usage and a better track record we should reconsider.
@geeknoid - can you enumerate exactly which APIs you hope to bring over?
The text was updated successfully, but these errors were encountered: