Repository navigation
Closed
Closed
Copy link
Labels
Milestone
Description
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:
SYSLIB1022diagnostic 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
[-]LoggerMessage - Add Diagnostic - Can't have malformed format strings[/-][+]LoggerMessage - Add diagnostic - Can't have malformed format strings[/+]1 remaining item
[-]LoggerMessage - Add diagnostic - Can't have malformed format strings[/-][+][LSG] LoggerMessage - Add diagnostic - Can't have malformed format strings[/+]allantargino commentedon Jan 20, 2023
Proposal
The proposed diagnostic descriptor would be:
With the following title:
And the following message format:
Code Sample
The diagnostic would be triggered for case such as the following ones:
terrajobst commentedon Jan 24, 2023
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.
diagnostic to detect malformed format strings in lsg template message
diagnostic to detect malformed format strings in lsg template message (…