You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
LoggerMessage source generator does not work with logger from primary constructor. #91121
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
Reacted by Filip Mestanek, Ch13, ribeach, Jacob Bundgaard and Sibusten
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.
@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.
Activity
tarekgh commentedon Aug 25, 2023
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
sloncho commentedon Jan 29, 2024
@tarekgh 8.0 is now GA and long-term support. Fixing this for 9.0 will leave more than 2 years with unsupported code.
tarekgh commentedon Jan 29, 2024
@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.
timia2109 commentedon Jan 29, 2024
Or you could do something like this: