Skip to content
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

[LSG] LoggerMessage - Add diagnostic when unexpected order provided in message template #52225

Closed
Tracked by #52549
maryamariyan opened this issue May 4, 2021 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-Extensions-Logging
Milestone

Comments

@maryamariyan
Copy link
Member

maryamariyan commented May 4, 2021

Today, using the LoggerMessage attribute, we can support mismatched orders, like:

[LoggerMessage(.., Message="message with foo: {foo}, and bar: {bar}"]
public void LogMethod(..., string bar, string foo)

But this is not supported with LoggerMessage.Define. The best way to keep the feature set coherent here is to add an INFO diagnostic for LoggerMessage source generator, and mention that "the order from message template arguments did not match the expected order from the method arguments.

Proposal

The proposed diagnostic descriptor would be:

public static DiagnosticDescriptor ParametersOutOfOrder { get; } = new DiagnosticDescriptor(
    id: "SYSLIB1026",
    title: new LocalizableResourceString(nameof(SR.ParametersOutOfOrderTitle), SR.ResourceManager, typeof(FxResources.Microsoft.Extensions.Logging.Generators.SR)),
    messageFormat: new LocalizableResourceString(nameof(SR.ParametersOutOfOrderMessage), SR.ResourceManager, typeof(FxResources.Microsoft.Extensions.Logging.Generators.SR)),
    category: "LoggingGenerator",
    DiagnosticSeverity.Info,
    isEnabledByDefault: true);

With the following title:

Logging method contains parameters out of order provided in the message template

And the following message format:

Logging method '{0}' contains parameters out of order provided in the message template

Code Sample

The diagnostic would be triggered for case such as the following ones:

IReadOnlyList<Diagnostic> diagnostics = await RunGenerator(@"
    partial class C
    {
        [LoggerMessage(EventId = 0, Level = LogLevel.Debug, Message=""{a3},{a1},{a4},{a2}"")]
        static partial void M1(ILogger logger, int a1, int a2, int a3, int a4);

        [LoggerMessage(EventId = 1, Message=""{a2},{a3},{a1}"")]
        static partial void M2(ILogger logger, int a1, LogLevel l, int a2, System.Exception e, int a3);
    }
");
@maryamariyan maryamariyan added this to the 6.0.0 milestone May 4, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label May 4, 2021
@ghost
Copy link

ghost commented May 4, 2021

Tagging subscribers to this area: @maryamariyan
See info in area-owners.md if you want to be subscribed.

Issue Details

Today, using the LoggerMessage attribute, we can support mismatched orders, like:

[LoggerMessage(.., Message="message with foo: {foo}, and bar: {bar}"]
public void LogMethod(..., string bar, string foo)

But this is not supported with LoggerMessage.Define. The best way to keep the feature set coherent here is to add an INFO diagnostic for LoggerMessage source generator, and mention that "the order from message template arguments did not match the expected order from the method arguments.

Author: maryamariyan
Assignees: -
Labels:

area-Extensions-Logging

Milestone: 6.0.0

@maryamariyan maryamariyan removed the untriaged New issue has not been triaged by the area owner label May 4, 2021
@maryamariyan maryamariyan modified the milestones: 6.0.0, 7.0.0 Aug 6, 2021
@eerhardt eerhardt modified the milestones: 7.0.0, Future Jul 14, 2022
@maryamariyan maryamariyan modified the milestones: Future, 8.0.0 Sep 28, 2022
@maryamariyan maryamariyan changed the title LoggerMessage - Add diagnostic when unexpected order provided in message template [LSG] LoggerMessage - Add diagnostic when unexpected order provided in message template Nov 16, 2022
allantargino added a commit to allantargino/dotnet-runtime that referenced this issue Jan 21, 2023
@allantargino
Copy link
Contributor

Proposal

The proposed diagnostic descriptor would be:

public static DiagnosticDescriptor ParametersOutOfOrder { get; } = new DiagnosticDescriptor(
    id: "SYSLIB1026",
    title: new LocalizableResourceString(nameof(SR.ParametersOutOfOrderTitle), SR.ResourceManager, typeof(FxResources.Microsoft.Extensions.Logging.Generators.SR)),
    messageFormat: new LocalizableResourceString(nameof(SR.ParametersOutOfOrderMessage), SR.ResourceManager, typeof(FxResources.Microsoft.Extensions.Logging.Generators.SR)),
    category: "LoggingGenerator",
    DiagnosticSeverity.Info,
    isEnabledByDefault: true);

With the following title:

Logging method contains parameters out of order provided in the message template

And the following message format:

Logging method '{0}' contains parameters out of order provided in the message template

Code Sample

The diagnostic would be triggered for case such as the following ones:

IReadOnlyList<Diagnostic> diagnostics = await RunGenerator(@"
    partial class C
    {
        [LoggerMessage(EventId = 0, Level = LogLevel.Debug, Message=""{a3},{a1},{a4},{a2}"")]
        static partial void M1(ILogger logger, int a1, int a2, int a3, int a4);

        [LoggerMessage(EventId = 1, Message=""{a2},{a3},{a1}"")]
        static partial void M2(ILogger logger, int a1, LogLevel l, int a2, System.Exception e, int a3);
    }
");

@tarekgh tarekgh added api-ready-for-review API is ready for review, it is NOT ready for implementation blocking Marks issues that we want to fast track in order to unblock other important work labels Jan 22, 2023
@terrajobst
Copy link
Member

terrajobst commented Jan 24, 2023

Video

We concluded that since the source generator already does the right thing, we don't need to add the diagnostic. While the old model was positional, there is no confusion because the old model had no named parameter. We believe this is good enough to avoid confusion. We don't need to force users of the source generator to be positional.

@terrajobst terrajobst closed this as not planned Won't fix, can't repro, duplicate, stale Jan 24, 2023
@teo-tsirpanis teo-tsirpanis added api-suggestion Early API idea and discussion, it is NOT ready for implementation and removed blocking Marks issues that we want to fast track in order to unblock other important work api-ready-for-review API is ready for review, it is NOT ready for implementation labels Jan 24, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Feb 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-Extensions-Logging
Projects
None yet
Development

No branches or pull requests

6 participants