dotnet / runtime

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

Logging source generator: CS9107 ILogger parameter is captured when use ILogger without generic #108264

Open universorum opened 3 days ago

universorum commented 3 days ago

Description

When use the LoggerMessage attribute with ILogger in primary constructors, compiler hint CS9107 if ILogger also pass to the base class. ILogger<T> does not affected.

Reproduction Steps

public class MyServiceTemplate(ILogger logger);

public partial class MyService1(ILogger logger) : MyServiceTemplate(logger) // CS9107
{
    [LoggerMessage(LogLevel.Error, "Log")]
    private partial void Log();
}

public partial class MyService2(ILogger<MyService2> logger) : MyServiceTemplate(logger)
{
    [LoggerMessage(LogLevel.Error, "Log")]
    private partial void Log();
}

Expected behavior

Both code will have same result: CS9107 or none.

Actual behavior

ILogger => CS9107 ILogger\<T> => None

Regression?

No response

Known Workarounds

No response

Configuration

SDK 9.0.100-rc.1.24452.12 MSBuild 17.12.0-preview-24422-09+d17ec720d macOS 15 Rider 2024.3 EAP 1

Other information

Generated code

// <auto-generated/>
#nullable enable

namespace ConsoleLab
{
    partial class MyService2
    {
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "9.0.10.43107")]
        private static readonly global::System.Action<global::Microsoft.Extensions.Logging.ILogger, global::System.Exception?> __LogCallback =
            global::Microsoft.Extensions.Logging.LoggerMessage.Define(global::Microsoft.Extensions.Logging.LogLevel.Error, new global::Microsoft.Extensions.Logging.EventId(1579320335, nameof(Log)), "Log", new global::Microsoft.Extensions.Logging.LogDefineOptions() { SkipEnabledCheck = true }); 

        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "9.0.10.43107")]
        private partial void Log()
        {
            if (logger.IsEnabled(global::Microsoft.Extensions.Logging.LogLevel.Error))
            {
                __LogCallback(logger, null);
            }
        }
    }
}
namespace ConsoleLab
{
    partial class MyService1
    {
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "9.0.10.43107")]
        private static readonly global::System.Action<global::Microsoft.Extensions.Logging.ILogger, global::System.Exception?> __LogCallback =
            global::Microsoft.Extensions.Logging.LoggerMessage.Define(global::Microsoft.Extensions.Logging.LogLevel.Error, new global::Microsoft.Extensions.Logging.EventId(1579320335, nameof(Log)), "Log", new global::Microsoft.Extensions.Logging.LogDefineOptions() { SkipEnabledCheck = true }); 

        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Extensions.Logging.Generators", "9.0.10.43107")]
        private partial void Log()
        {
            if (logger.IsEnabled(global::Microsoft.Extensions.Logging.LogLevel.Error))
            {
                __LogCallback(logger, null);
            }
        }
    }
}
dotnet-policy-service[bot] commented 3 days ago

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