extensions icon indicating copy to clipboard operation
extensions copied to clipboard

Compile error when using `Microsoft.Extensions.Telemetry.Abstractions` in `.NETStandard2.1` projects

Open xiaomi7732 opened this issue 4 months ago • 3 comments

Description

Try to use the same code, multi-targeting .NET 6 and .NETStandard2.1, build failure on netstandard.

Image

Reproduction Steps

Here's the source code for a repro:

using Microsoft.Extensions.Logging;

struct TestObject
{
  public int Id { get; set; }
}

internal static partial class TestObjExtensions
{
  [LoggerMessage(Level = LogLevel.Information, Message = "Logging TestObject with Id")]
  public static partial void LogTestObject(this ILogger logger, [LogProperties(OmitReferenceName = true)] in TestObject testObject);
}

Project file, multi-targeting for 2 Fx:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <LangVersion>10.0</LangVersion>
    <TargetFrameworks>net6.0;netstandard2.1</TargetFrameworks>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Telemetry.Abstractions" Version="9.8.0" />
  </ItemGroup>

</Project>

Expected behavior

Microsoft.Extensions.Telemetry.Abstractions supports .NETStandard 2.1 like on NuGet page:

Image

Actual behavior

Build warning + error:

LearnLoggingComplexObjects netstandard2.1 failed with 1 error(s) and 1 warning(s) (0.2s)
    C:\Demo\LearnLoggingComplexObjects\TestObj.cs(11,121): warning SYSLIB1015: Argument 'testObject' is not referenced from the logging message (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1015)
    C:\Demo\LearnLoggingComplexObjects\obj\Debug\netstandard2.1\Microsoft.Gen.Logging\Microsoft.Gen.Logging.LoggingGenerator\Logging.g.cs(11,32): error CS0757: A partial method may not have multiple implementing declarations
  LearnLoggingComplexObjects net6.0 succeeded (0.8s) → bin\Debug\net6.0\LearnLoggingComplexObjects.dll

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

xiaomi7732 avatar Aug 20 '25 21:08 xiaomi7732

I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  • https://github.com/dotnet/extensions/issues/4565

MihuBot avatar Aug 20 '25 21:08 MihuBot

Same issue, Version 9.7.0 works, 9.8.0 is broken, I am only targeting .net standard 2.0. What catched my eye is that since version 9.8.0 .net standard 2.0 has become a target framework.

SIT-jekleine avatar Aug 25 '25 09:08 SIT-jekleine

So, the thing is that both Microsoft.Gen.Logging.LoggingGenerator and Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator kick in in case of targeting .NET Standard 2.1.

@xiaomi7732 as a workaround you can add <DisableMicrosoftExtensionsLoggingSourceGenerator>true</DisableMicrosoftExtensionsLoggingSourceGenerator> in your .csproj file, it will disable "built-in" source-gen of Microsoft.Extensions.Logging.

@joperezr does this issue look like a special/corner case of #4286 ?

Just in case, here's a minimal repro:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <LangVersion>10.0</LangVersion>
    <TargetFrameworks>netstandard2.1</TargetFrameworks>
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Telemetry.Abstractions" Version="9.8.0" />
  </ItemGroup>
</Project>

xakep139 avatar Oct 24 '25 15:10 xakep139