Skip to content

[LSG] Logging Source Generator fails to compile when out parameter modifier is present #64665

@maryamariyan

Description

@maryamariyan

As part of issue #62644 we fixed compile errors with using ref or in.

But for out usage, we would need to add a diagnostic that explains out usage is not supported for LoggerMessageAttribute annotated logging methods.

Proposal

The proposed diagnostic descriptor would be:

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

With the following title:

Argument is using the unsupported out parameter modifier

And the following message format:

Argument '{0}' is using the unsupported out parameter modifier

Code Sample

IReadOnlyList<Diagnostic> diagnostics = await RunGenerator(@$"
                partial class C
                {{
                    [LoggerMessage(EventId = 0, Level = LogLevel.Debug, Message = ""Parameter {{P1}}"")]
                    static partial void M(ILogger logger, out int p1);
                }}");

Assert.Single(diagnostics);
Assert.Equal(DiagnosticDescriptors.InvalidLoggingMethodParameterOut.Id, diagnostics[0].Id);

TODO:

  • Take diagnostic message/severity/category to API review

Metadata

Metadata

Assignees

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
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions