dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.29k stars 4.74k forks source link

Source generation error in LoggerMessageAttribute in Microsoft.Extensions.Logging.Abstractions #91678

Open abrca opened 1 year ago

abrca commented 1 year ago

Description

Using 'params' keyword in method parameters "params object?[] args" in partial class under [LoggerMessage] attribute cause Compiler Error CS0758 - in generated code 'params' keyword is absent

Reproduction Steps

using Microsoft.Extensions.Logging;

internal static partial class LogMessages
{
    [LoggerMessage(Message = "Message: {message}, {args}", Level = LogLevel.Information)]
    internal static partial void MSG(this ILogger logger, string? message, params object?[] args);
}

Expected behavior

Compile

Actual behavior

in source-generated file LoggerMessage.g.cs, 'params' keyword is absent in method parameters "global::System.Object?[] args"

partial class LogMessages
{
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "7.0.7.1805")]
    private static readonly global::System.Action<global::Microsoft.Extensions.Logging.ILogger, global::System.String?, global::System.Object?[], global::System.Exception?> __MSGCallback =
        global::Microsoft.Extensions.Logging.LoggerMessage.Define<global::System.String?, global::System.Object?[]>(global::Microsoft.Extensions.Logging.LogLevel.Information, new global::Microsoft.Extensions.Logging.EventId(-1, nameof(MSG)), "Message: {message}, {args}", new global::Microsoft.Extensions.Logging.LogDefineOptions() { SkipEnabledCheck = true }); 

    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "7.0.7.1805")]
    internal static partial void MSG(this global::Microsoft.Extensions.Logging.ILogger logger, global::System.String? message, global::System.Object?[] args)
    {
        if (logger.IsEnabled(global::Microsoft.Extensions.Logging.LogLevel.Information))
        {
            __MSGCallback(logger, message, args, null);
        }
    }
}

Regression?

No response

Known Workarounds

No response

Configuration

.NET 7.0 Windows 10 x64 Visual Studio Community 2022

Other information

No response

ghost commented 1 year ago

Tagging subscribers to this area: @dotnet/area-extensions-logging See info in area-owners.md if you want to be subscribed.

Issue Details
### Description Using 'params' keyword in method parameters "params object?[] args" in partial class under [LoggerMessage] attribute cause Compiler Error CS0758 - in generated code 'params' keyword is absent ### Reproduction Steps ``` using Microsoft.Extensions.Logging; internal static partial class LogMessages { [LoggerMessage(Message = "Message: {message}, {args}", Level = LogLevel.Information)] internal static partial void MSG(this ILogger logger, string? message, params object?[] args); } ``` ### Expected behavior Compile ### Actual behavior in source-generated file LoggerMessage.g.cs, 'params' keyword is absent in method parameters "global::System.Object?[] args" ``` partial class LogMessages { [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "7.0.7.1805")] private static readonly global::System.Action __MSGCallback = global::Microsoft.Extensions.Logging.LoggerMessage.Define(global::Microsoft.Extensions.Logging.LogLevel.Information, new global::Microsoft.Extensions.Logging.EventId(-1, nameof(MSG)), "Message: {message}, {args}", new global::Microsoft.Extensions.Logging.LogDefineOptions() { SkipEnabledCheck = true }); [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "7.0.7.1805")] internal static partial void MSG(this global::Microsoft.Extensions.Logging.ILogger logger, global::System.String? message, global::System.Object?[] args) { if (logger.IsEnabled(global::Microsoft.Extensions.Logging.LogLevel.Information)) { __MSGCallback(logger, message, args, null); } } } ``` ### Regression? _No response_ ### Known Workarounds _No response_ ### Configuration .NET 7.0 Windows 10 x64 Visual Studio Community 2022 ### Other information _No response_
Author: abrca
Assignees: -
Labels: `untriaged`, `area-Extensions-Logging`
Milestone: -
ericstj commented 3 months ago

Maybe we could emit a better message here, but I don't think params are supported or useful here. The way they're listed in the template wouldn't format the actual values.