Skip to content

[LSG] LoggerMessage - Add diagnostic - Can't have the same template with different casing #52228

@maryamariyan

Description

@maryamariyan

LoggerMessage supports case insensitive parameters. But we need to add a diagnostic when different casing of the same parameter is specified in the same message template like in the below sample:

[LoggerMessage(EventId = 0, Level = LogLevel.Debug, Message = ""M1 {p1} {P1}"")]
public static partial void M1(ILogger logger, int p1, int P1);

Refer to: #51064 (comment)

  • Note: case insensitive support against on LoggerMessage.Define is also supported.

Proposal

The proposed diagnostic descriptor would be:

public static DiagnosticDescriptor InconsistentTemplateCasing { get; } = new DiagnosticDescriptor(
    id: "SYSLIB1021",
    title: new LocalizableResourceString(nameof(SR.InconsistentTemplateCasingTitle), SR.ResourceManager, typeof(FxResources.Microsoft.Extensions.Logging.Generators.SR)),
    messageFormat: new LocalizableResourceString(nameof(SR.InconsistentTemplateCasingMessage), SR.ResourceManager, typeof(FxResources.Microsoft.Extensions.Logging.Generators.SR)),
    category: "LoggingGenerator",
    DiagnosticSeverity.Error,
    isEnabledByDefault: true);

With the following title:

Logging method have the same template with different casing

And the following message format:

Logging method '{0}' have the same template with different casing

Note: SYSLIB1021 diagnostic descriptor is already merged on main, but it is not being triggered.

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 = ""M1 {par1} {PAr1} {a}"")]
        static partial void M1(ILogger logger, int par1, int a);
    }
");

Metadata

Metadata

Assignees

Labels

api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-Extensions-Loggingin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions