Skip to content

Compilation error when using Microsoft.Extensions.Telemetry.Abstractions with multiple TFMs #7061

@evgenyfedorov2

Description

@evgenyfedorov2

Description

Compilation error when using multi targeting such as .NET 10 and NET 9.0 or NET 8.0 or both of them.

Image

Reproduction Steps

csproj file:

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

  <PropertyGroup>
    <TargetFrameworks>net9.0;net10.0</TargetFrameworks>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
	  <OutputType>Exe</OutputType>
  </PropertyGroup>

	<ItemGroup>
		<PackageReference Include="Microsoft.Extensions.Telemetry.Abstractions" Version="10.0.0" />
		<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.0" />
		<FrameworkReference Include="Microsoft.AspNetCore.App" />
	</ItemGroup>
</Project>

Program.cs:

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Logging;

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();
app.MapGet("/index", () =>
{
    using var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
    var logger = loggerFactory.CreateLogger("repro");

    Log.Hello(logger, "hi");
});

app.Run();


internal static partial class Log
{
    [LoggerMessage(LogLevel.Information, "Hello {Name}.")]
    internal static partial void Hello(ILogger logger, string name);
}

Expected behavior

Microsoft.Extensions.Logging.Abstractions does not kick in, code compiles and works correctly

Actual behavior

Compilation error CS0757 A partial method may not have multiple implementing declarations

Regression?

No response

Known Workarounds

Add this to csproj file:

	<Target Name="RemoveLoggingAnalyzers" BeforeTargets="CoreCompile">
		<ItemGroup>
			<Analyzer Remove="@(Analyzer)" Condition="'%(Analyzer.AssemblyName)' == 'Microsoft.Extensions.Logging.Generators' Or '%(Analyzer.NuGetPackageId)' == 'Microsoft.Extensions.Logging.Abstractions'" />
		</ItemGroup>
	</Target>

Configuration

.NET 10 and multi targeting build with previous .NET such as 8.0 and/or 9.0

Other information

No response

Metadata

Metadata

Assignees

Labels

area-telemetrybugThis issue describes a behavior which is not expected - a bug.untriaged

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions