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 classlibdotnet add package Microsoft.Extensions.Logging --version 8.0.0-preview.7.23375.6
Add the following c# file to the project.
usingMicrosoft.Extensions.Logging;namespaceTest;publicpartialclassLoggerFromPrimaryConstructor(ILogger<LoggerFromPrimaryConstructor>logger){[LoggerMessage(Level=LogLevel.Debug,Message="This is a test")]publicpartialvoidLogTest();}Expected behavior
The source generator emits code with similar behaviour as if I had written the following.
publicpartialclassLogWithExplicitProperty{privatereadonlyILogger<LoggerFromPrimaryConstructor>logger;publicLogWithExplicitProperty(ILogger<LoggerFromPrimaryConstructor>logger){this.logger=logger;}[LoggerMessage(Level=LogLevel.Debug,Message="This is a test")]publicpartialvoidLogTest();}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
Description
The source generator for
LoggerMessagedoes not work if the logger is injected from the primary constructor.Reproduction Steps
Create the project.
dotnet new classlibdotnet add package Microsoft.Extensions.Logging --version 8.0.0-preview.7.23375.6Add the following c# file to the project.
Expected behavior
The source generator emits code with similar behaviour as if I had written the following.
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