Skip to content

LoggerMessage source generator does not work with logger from primary constructor. #91121

Closed

Description

Description

The source generator for LoggerMessage does not work if the logger is injected from the primary constructor.

Reproduction Steps

Create the project.

  • dotnet new classlib
  • dotnet add package Microsoft.Extensions.Logging --version 8.0.0-preview.7.23375.6

Add the following c# file to the project.

using Microsoft.Extensions.Logging;

namespace Test;

public partial class LoggerFromPrimaryConstructor(ILogger<LoggerFromPrimaryConstructor> logger)
{
    [LoggerMessage(Level = LogLevel.Debug, Message = "This is a test")]
    public partial void LogTest();
}

Expected behavior

The source generator emits code with similar behaviour as if I had written the following.

public partial class LogWithExplicitProperty
{
    private readonly ILogger<LoggerFromPrimaryConstructor> logger;

    public LogWithExplicitProperty(ILogger<LoggerFromPrimaryConstructor> logger)
    {
        this.logger = logger;
    }

    [LoggerMessage(Level = LogLevel.Debug, Message = "This is a test")]
    public partial void LogTest();
}

Actual behavior

Compilation fails with error SYSLIB1019: Couldn't find a field of type Microsoft.Extensions.Logging.ILogger in class LoggerFromPrimaryConstructor.

I also got error CS1525: Invalid expression term '<' in another project where the source generator did find the logger for some reason, but generated incorrect code that referenced the logger by the symbol <logger>P. I assume this is the generated field name? I could not reproduce it however.

Regression?

This is not a regression, as primary constructors are not GA.

Known Workarounds

Use a regular constructor instead of a primary constructor.

Configuration

I am using .NET 8 preview 7 on Windows 10 Enterprise 22H2 x64.

Other information

No response

Activity

  1. ghost added
    untriaged
    on Aug 25, 2023
  2. tarekgh commented on Aug 25, 2023

    @tarekgh
    Member

    Yes, the primary constructors feature is a new compiler feature and is not supported yet with the logging source gen. This is something we can consider supporting in future releases. Thanks for your report.

    CC @geeknoid

  3. added this to the 9.0.0 milestone on Aug 25, 2023
  4. ghost removed
    untriaged
    on Aug 25, 2023
  5. sloncho commented on Jan 29, 2024

    @sloncho

    @tarekgh 8.0 is now GA and long-term support. Fixing this for 9.0 will leave more than 2 years with unsupported code.

  6. tarekgh commented on Jan 29, 2024

    @tarekgh
    Member

    @sloncho we usually get issues after facts which we address it in the next releases. If we find demands on specific issue, we consider porting the fix back to the service release.

  7. timia2109 commented on Jan 29, 2024

    @timia2109

    Or you could do something like this:

    public partial class Foo(ILogger<Foo> logger) {
        private readonly ILogger _logger = logger;
    }
  8. added
    in-pr
    on Apr 28, 2024
  9. locked and limited conversation to collaborators on Jun 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Extensions-Loggingin-pr

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions