-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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 - Can't have malformed format strings #52226
Comments
Tagging subscribers to this area: @maryamariyan Issue DetailsCases for which this diagnostic would be helpful: [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);
|
ProposalThe 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:
And the following message format:
Code SampleThe 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);
}
"); |
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. |
Refer to: #51064 (comment)
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:
The text was updated successfully, but these errors were encountered: