Skip to content

[LSG] LoggerMessage - Add diagnostic - Can't have malformed format strings #52226

Closed

Description

@maryamariyan

Refer to: #51064 (comment)

Proposal

The proposed diagnostic descriptor would be:

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

With the following title:

Logging method contains malformed format strings

And the following message format:

Logging method '{0}' contains malformed format strings

Note: SYSLIB1022 diagnostic 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 = 1, Level = LogLevel.Debug, Message = ""M1 {A} M1 { M3"")]
        static partial void M1(ILogger logger);

        [LoggerMessage(EventId = 2, Level = LogLevel.Debug, Message = ""M2 {A} M2 } M2"")]
        static partial void M2(ILogger logger);

        [LoggerMessage(EventId = 3, Level = LogLevel.Debug, Message = ""M3 {arg1"")]
        static partial void M3(ILogger logger);

        [LoggerMessage(EventId = 4, Level = LogLevel.Debug, Message = ""M4 arg1}"")]
        static partial void M4(ILogger logger);

        [LoggerMessage(EventId = 5, Level = LogLevel.Debug, Message = ""M5 {"")]
        static partial void M5(ILogger logger);

        [LoggerMessage(EventId = 6, Level = LogLevel.Debug, Message = ""}M6 "")]
        static partial void M6(ILogger logger);

        [LoggerMessage(EventId = 7, Level = LogLevel.Debug, Message = ""M7 {{arg1}}"")]
        static partial void M7(ILogger logger);

        [LoggerMessage(EventId = 8, Level = LogLevel.Debug, Message = ""}M8{arg1}{arg2}"")]
        static partial void M8(ILogger logger);
    }
");

Activity

  1. ghost added
    untriaged
    on May 4, 2021
  2. removed
    untriaged
    on May 4, 2021
  3. added this to the 6.0.0 milestone on May 4, 2021
  4. ghost added
    in-pr
    on May 4, 2021
  5. changed the title [-]LoggerMessage - Add Diagnostic - Can't have malformed format strings[/-] [+]LoggerMessage - Add diagnostic - Can't have malformed format strings[/+] on May 4, 2021
  6. ghost removed
    in-pr
    on May 25, 2021
  7. modified the milestones: 6.0.0, 7.0.0 on Aug 12, 2021
  8. modified the milestones: 7.0.0, Future on Jul 14, 2022
  9. removed this from the Future milestone on Sep 28, 2022
  10. 1 remaining item

  11. changed the title [-]LoggerMessage - Add diagnostic - Can't have malformed format strings[/-] [+][LSG] LoggerMessage - Add diagnostic - Can't have malformed format strings[/+] on Nov 16, 2022
  12. allantargino commented on Jan 20, 2023

    @allantargino
    Contributor

    Proposal

    The proposed diagnostic descriptor would be:

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

    With the following title:

    Logging method contains malformed format strings
    

    And the following message format:

    Logging method '{0}' contains malformed format strings
    

    Note: SYSLIB1022 diagnostic 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 = 1, Level = LogLevel.Debug, Message = ""M1 {A} M1 { M3"")]
            static partial void M1(ILogger logger);
    
            [LoggerMessage(EventId = 2, Level = LogLevel.Debug, Message = ""M2 {A} M2 } M2"")]
            static partial void M2(ILogger logger);
    
            [LoggerMessage(EventId = 3, Level = LogLevel.Debug, Message = ""M3 {arg1"")]
            static partial void M3(ILogger logger);
    
            [LoggerMessage(EventId = 4, Level = LogLevel.Debug, Message = ""M4 arg1}"")]
            static partial void M4(ILogger logger);
    
            [LoggerMessage(EventId = 5, Level = LogLevel.Debug, Message = ""M5 {"")]
            static partial void M5(ILogger logger);
    
            [LoggerMessage(EventId = 6, Level = LogLevel.Debug, Message = ""}M6 "")]
            static partial void M6(ILogger logger);
    
            [LoggerMessage(EventId = 7, Level = LogLevel.Debug, Message = ""M7 {{arg1}}"")]
            static partial void M7(ILogger logger);
    
            [LoggerMessage(EventId = 8, Level = LogLevel.Debug, Message = ""}M8{arg1}{arg2}"")]
            static partial void M8(ILogger logger);
        }
    ");
  13. added
    blocking
    api-ready-for-review
    on Jan 22, 2023
  14. terrajobst commented on Jan 24, 2023

    @terrajobst
    Contributor

    Video

    Looks good as proposed (severity, category, ID). We should also change the generator to not emit any code rather than emitting code that won't compile with an unintelligent error.

  15. added
    api-approved
    and removed
    api-ready-for-review
    on Jan 24, 2023
  16. removed
    blocking
    on Jan 24, 2023
  17. added a commit that references this issue on Feb 1, 2023
    5f011c6
  18. ghost added
    in-pr
    on Feb 1, 2023
  19. ghost removed
    in-pr
    on Feb 5, 2023
  20. ghost locked as resolved and limited conversation to collaborators on Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-Extensions-Logging

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions