extensions: `Microsoft.Extensions.Http.Resilience` breaks `LoggerMessage` code generation.
Description
We have a .NET 8 project where we add a reference to a package that includes a dependency on Microsoft.Extensions.Http.Resilience. That project also uses [LoggerMessage] to take advantage of the source generator.
That project also includes a reference to a package (.NET Standard 2.0) that is using il-repack, which results in all the types under Microsoft.Extensions.Logging (including Microsoft.Extensions.Logging.LoggerMessageAttribute) being included in the assembly as internal types.
In this scenario, the source generator does not work.
[LoggerMessage(Level = LogLevel.Information, Message = "Something")]
private static partial void LogSomething(ILogger logger);
The error is:
CS8795 Partial method 'Program.LogSomething(ILogger)' must have an implementation part because it has accessibility modifiers.
Investigating this, I believe that the problem is related with how Microsoft.Gen.Logging.LoggingGenerator.LoggingGenerator is looking up for Microsoft.Extensions.Logging.LoggerMessageAttribute.
I think that Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator.Parser did that lookup considering the visibility of the types:
INamedTypeSymbol bestTypeByMetadataName = _compilation.GetBestTypeByMetadataName("Microsoft.Extensions.Logging.LoggerMessageAttribute");
I’m not 100% sure, but it looks like Microsoft.Gen.Logging.LoggingGenerator.LoggingGenerator isn’t considering visibility. Which results in the code not being generated.
Reproduction Steps
Here’s the csproj definition:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Cegid.Hydrogen.Hosting" Version="8.0.5" />
<PackageReference Include="Cegid.Lithium.Settings.Client.Rest.SingleAssembly" Version="6.0.0" />
</ItemGroup>
</Project>
Cegid.Hydrogen.Hosting is the package that depends on Microsoft.Extensions.Http.Resilience.
Cegid.Lithium.Settings.Client.Rest.SingleAssembly is the package that uses il-repack and has LoggerMessageAttribute as an internal type.
Expected behavior
The source generator should work as expected, because there is only one public type accessible named Microsoft.Extensions.Logging.LoggerMessageAttribute.
Actual behavior
The source generator does not kick-in.
Regression?
No.
Known Workarounds
Adding this in the csproj, after the package reference, makes the old source generator work:
<PropertyGroup>
<DisableMicrosoftExtensionsLoggingSourceGenerator>false</DisableMicrosoftExtensionsLoggingSourceGenerator>
</PropertyGroup>
Obviously, this does not work if we remove the reference to the single assembly, as BOTH source generators kick-in and we end up with 2 implementations of the logging method.
Configuration
We’re using .NET 8.0.4 and Microsoft.Extensions.Http.Resilience 8.4.0. Nothing else seams relevant for this issue.
Other information
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 20 (11 by maintainers)
Yes.
Hi @dariusclay.
Sorry for the delay. I tested the scenario with the package that you refer and it fixes it.
Will this be published in version 8.9.0?
@hugoqribeiro Yes, this will be part of the next monthly snapshot.
@dariusclay thank you for the explanation and the workaround! Even though there may be a good reason for it, I was surprised to read
A lot of people will be getting the
Microsoft.Extensions.Http.Resiliencepackage as part of adding Aspire.NET orchestration to their solution, and are not notified of this behavior change.Can you please move our comments to a new issue? I can only recreate my own comments but not move yours (I did not intend to hijack @hugoqribeiro’s issue, I thought they our issues were symptoms of the same cause).
You mean a simplified package after being processed with il-repack?
I believe that I can reproduce this without il-repack, if I add an internal type named
Microsoft.Extensions.Logging.LoggerMessageAttributein the original project…Will that help?