Skip to content

[LSG] Logging Source Generator fails to detect event name uniqueness within a class scope #64667

@maryamariyan

Description

@maryamariyan

As per design logging source generator provides a warning diagnostic when event ID is not unique within the scope of a class between log methods using LoggerMessageAttribute. It should also be able to set diagnostic message and detect when event name is being reused in the same scope.

Proposal

The proposed diagnostic descriptor would be:

public static DiagnosticDescriptor ShouldntReuseEventNames { get; } = new DiagnosticDescriptor(
    id: "SYSLIB1025",
    title: new LocalizableResourceString(nameof(SR.ShouldntReuseEventNamesTitle), SR.ResourceManager, typeof(FxResources.Microsoft.Extensions.Logging.Generators.SR)),
    messageFormat: new LocalizableResourceString(nameof(SR.ShouldntReuseEventNamesMessage), SR.ResourceManager, typeof(FxResources.Microsoft.Extensions.Logging.Generators.SR)),
    category: "LoggingGenerator",
    DiagnosticSeverity.Warning,
    isEnabledByDefault: true);

With the following title:

Multiple logging methods should not use the same event name within a class

And the following message format:

Multiple logging methods are using event name {0} in class {1}

Code Sample

IReadOnlyList<Diagnostic> diagnostics = await RunGenerator(@"
    partial class MyClass
    {
        [LoggerMessage(EventId = 0, EventName = ""MyEvent"", Level = LogLevel.Debug, Message = ""M1"")]
        static partial void M1(ILogger logger);

        [LoggerMessage(EventId = 1, EventName = ""MyEvent"", Level = LogLevel.Debug, Message = ""M1"")]
        static partial void M2(ILogger logger);
    }
");

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedarea-Extensions-LoggingblockingMarks issues that we want to fast track in order to unblock other important work

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions