Skip to content

Microsoft.Extensions.Http.Resilience breaks LoggerMessage code generation. #5173

Closed

Description

@hugoqribeiro

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.

capture

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

Activity

  1. added
    bug
    on May 22, 2024
  2. dariusclay commented on May 22, 2024

    @dariusclay
    Member

    Can you provide a minimal reproduction of this issue using il-repack? It would help with investigations.

  3. hugoqribeiro commented on May 22, 2024

    @hugoqribeiro
    Author

    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.LoggerMessageAttribute in the original project...

    Will that help?

  4. dariusclay commented on May 22, 2024

    @dariusclay
    Member

    With il-repack, it can be configured to not internalize specific types. Do you know if this is possible to use in your case?

     - /internalize[:<excludefile>]  sets all types but the ones from the first assembly 'internal'. <excludefile> contains one regex per
                                     line to compare against FullName of types NOT to internalize.
    
  5. hugoqribeiro commented on May 22, 2024

    @hugoqribeiro
    Author

    With il-repack, it can be configured to not internalize specific types. Do you know if this is possible to use in your case?

     - /internalize[:<excludefile>]  sets all types but the ones from the first assembly 'internal'. <excludefile> contains one regex per
                                     line to compare against FullName of types NOT to internalize.
    

    I'm unsure that having 2 public types with the same name will solve the issue.
    What I've tried was renaming Microsoft.Extensions.Logging to Cegid.Microsoft.Extensions.Logging, but that breaks the package at runtime (because il-repack is unable to do the replaces everywhere, for some reason that I didn't understand (yet).

    Let me try reproducing the issue without il-repack...

  6. hugoqribeiro commented on May 22, 2024

    @hugoqribeiro
    Author

    Can you provide a minimal reproduction of this issue using il-repack? It would help with investigations.

    OK. This is reproducible by referencing a simple client library.

    Here's a solution that reproduces the problem:

    Repo: https://github.com/hugoqribeiro/issues
    Solution: https://github.com/hugoqribeiro/issues/tree/main/src/2024.05.Microsoft.Extensions.Http.Resilience

  7. dariusclay commented on May 22, 2024

    @dariusclay
    Member

    Thanks for the repro

  8. dariusclay commented on May 22, 2024

    @dariusclay
    Member

    I'm unsure that having 2 public types with the same name will solve the issue.

    There's also this /union but not sure how that will look either without actually trying it.

  9. hansmbakker commented on Jul 31, 2024

    @hansmbakker
  10. hugoqribeiro commented on Aug 1, 2024

    @hugoqribeiro
    Author

    Hi @dariusclay? No news on this issue?

    It keeps coming back as more and more of our projects that the dependency that references Microsoft.Extensions.Http.Resilience.

  11. dariusclay commented on Aug 2, 2024

    @dariusclay
  12. 3 remaining items

  13. dariusclay commented on Aug 2, 2024

    @dariusclay
    Member

    @hugoqribeiro note that this will occur if any of the following types are internalized by il-repack

    "Microsoft.Extensions.Logging.LoggerMessageAttribute"
    "Microsoft.Extensions.Logging.LogPropertiesAttribute"
    "Microsoft.Extensions.Logging.TagProviderAttribute"
    "Microsoft.Extensions.Logging.TagNameAttribute"
    "Microsoft.Extensions.Logging.LogPropertyIgnoreAttribute"
    "Microsoft.Extensions.Logging.ITagCollector"
    "Microsoft.Extensions.Logging.ILogger"
    "Microsoft.Extensions.Logging.LogLevel"
    "Microsoft.Extensions.Compliance.Classification.DataClassificationAttribute"
    "Microsoft.Extensions.Compliance.Classification.NoDataClassificationAttribute"
    
  14. hugoqribeiro commented on Aug 4, 2024

    @hugoqribeiro
    Author

    @dariusclay, thanks. I'm waiting for the guy that implemented the il-repack thing to come back from vacations. :)

  15. hugoqribeiro commented on Aug 13, 2024

    @hugoqribeiro
    Author

    Hi @dariusclay, we tested union and it doesn't solve the issue. There is still an internal ILogger that causes the issue.

    I see you merged a fix into main. Does that mean that it will be released in 8.x anytime soon?

    Thanks.

  16. geeknoid commented on Aug 13, 2024

    @geeknoid
    Member

    @hugoqribeiro Yes, this will be part of the next monthly snapshot.

  17. dariusclay commented on Aug 14, 2024

    @dariusclay
    Member

    @hugoqribeiro can you test the fix using our nightlies? I did a test run using my local repro but it will be good to know if it works in the wild.

    Update your nuget package sources to include our public nightly feed:
    https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json

    Add a reference to this package:
    <PackageReference Include="Microsoft.Extensions.Telemetry.Abstractions" Version="8.8.0-preview.24413.3" />

  18. hugoqribeiro commented on Aug 22, 2024

    @hugoqribeiro
    Author

    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?

  19. RussKie commented on Aug 23, 2024

    @RussKie
    Contributor

    Will this be published in version 8.9.0?

    Yes.

  20. locked and limited conversation to collaborators on Sep 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-fundamentalsbugwork in progress 🚧

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions