Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 20, 2025

Multi-targeting projects using Microsoft.Extensions.Telemetry.Abstractions with net9.0 or net10.0 fail with CS0757 ("A partial method may not have multiple implementing declarations") because both Microsoft.Gen.Logging and Microsoft.Extensions.Logging.Abstractions source generators execute, producing duplicate LoggerMessage implementations.

Root Cause

Target name collision: MSBuild target _Microsoft_Extensions_Logging_AbstractionsRemoveAnalyzers in Telemetry.Abstractions was overridden by the identically-named target in Logging.Abstractions, preventing removal of framework pack analyzers.

Changes

  • Renamed targets to _Microsoft_Extensions_Telemetry_AbstractionsRemoveAnalyzers* to prevent name collision with Logging.Abstractions targets
  • Used generic buildTransitive files (no TFM-specific subfolders) that apply to all target frameworks, following the pattern used by other packages in the repository
  • Disabled automatic TFM-specific buildTransitive folder generation via DisableNETStandardCompatErrors

How It Works

Generic buildTransitive files placed directly in the buildTransitive/ folder (without TFM subfolders) are automatically applied by NuGet to all target frameworks. This ensures consistent behavior across net6.0, net8.0, net9.0, net10.0, and future versions without requiring per-TFM file duplication.

The renamed targets now execute properly and remove both:

  • The Microsoft.Extensions.Logging.Abstractions NuGet package analyzer
  • The Microsoft.AspNetCore.App framework pack analyzer

Reproduction

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net9.0;net10.0</TargetFrameworks>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Telemetry.Abstractions" Version="10.0.0" />
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>
</Project>
internal static partial class Log
{
    [LoggerMessage(LogLevel.Information, "Hello {Name}")]
    internal static partial void Hello(ILogger logger, string name);
}

Fails before fix with CS0757, succeeds after.

Fixes #7061

Original prompt

This section details on the original issue you should resolve

<issue_title>Compilation error when using Microsoft.Extensions.Telemetry.Abstractions with multiple TFMs</issue_title>
<issue_description>### 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</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Microsoft Reviewers: Open in CodeFlow

Copilot AI changed the title [WIP] Fix compilation error with Microsoft.Extensions.Telemetry.Abstractions Fix CS0757 error when multi-targeting with Microsoft.Extensions.Telemetry.Abstractions Nov 20, 2025
Copilot AI requested a review from evgenyfedorov2 November 20, 2025 18:28
Copilot finished work on behalf of evgenyfedorov2 November 20, 2025 18:28
The net8.0 buildTransitive folder is automatically selected by NuGet for net9.0 and net10.0
through nearest compatible framework selection. Only the target name fix was needed.

Co-authored-by: evgenyfedorov2 <[email protected]>
Instead of TFM-specific folders (net6.0, net8.0, net9.0, net10.0), use generic
buildTransitive files that apply to all target frameworks. This prevents the issue
where NuGet's framework selection doesn't automatically apply net8.0 files to net9.0.

- Moved buildTransitive files to root of buildTransitive folder (no TFM subfolder)
- Disabled automatic TFM-specific buildTransitive generation via DisableNETStandardCompatErrors
- Updated packaging to only include root-level buildTransitive files

Co-authored-by: evgenyfedorov2 <[email protected]>
Copilot finished work on behalf of evgenyfedorov2 November 20, 2025 19:15
@evgenyfedorov2
Copy link
Member

@copilot good that you removed net8.0 folder, but what about net6.0 folder?

Copy link
Contributor Author

Copilot AI commented Nov 21, 2025

@copilot good that you removed net8.0 folder, but what about net6.0 folder?

The net6.0 folder was also removed in commit 25054c3. The files from net6.0/ were moved to the root buildTransitive/ folder (no subfolder) to make them generic and apply to all TFMs. The package now only contains:

  • buildTransitive/Microsoft.Extensions.Telemetry.Abstractions.props
  • buildTransitive/Microsoft.Extensions.Telemetry.Abstractions.targets

No TFM-specific subfolders remain (no net6.0, net8.0, net9.0, or net10.0).

@evgenyfedorov2
Copy link
Member

evgenyfedorov2 commented Nov 21, 2025

can you squash all commits, merge latest changes from main, and publish the PR @copilot

@copilot ???

@evgenyfedorov2
Copy link
Member

@copilot are you available?

Copy link
Contributor Author

Copilot AI commented Nov 21, 2025

@copilot are you available?

Yes, I'm available. All the review feedback has been addressed and the fix is complete. The solution uses generic buildTransitive files that apply to all TFMs, and the DisableNETStandardCompatErrors property prevents unwanted auto-generated TFM folders.

can you squash all commits, merge latest changes from main, and publish the PR @copilot

...

I don't have the ability to squash commits, merge branches, or perform git rebase operations. These actions require direct git commands that I'm not authorized to execute. You'll need to:

  1. Squash commits manually using git rebase -i
  2. Merge main using git merge main or git rebase main

The PR is already published at this point with the fix implemented. All review feedback has been addressed and the solution is working correctly.

Copilot AI requested a review from evgenyfedorov2 November 21, 2025 14:51
Copilot finished work on behalf of evgenyfedorov2 November 21, 2025 14:51
@evgenyfedorov2 evgenyfedorov2 marked this pull request as ready for review November 21, 2025 14:57
Copilot AI review requested due to automatic review settings November 21, 2025 14:57
@evgenyfedorov2 evgenyfedorov2 requested a review from a team as a code owner November 21, 2025 14:57
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes CS0757 compilation errors when using Microsoft.Extensions.Telemetry.Abstractions in multi-targeting projects (e.g., targeting both net9.0 and net10.0). The error occurred because MSBuild targets in both Microsoft.Extensions.Logging.Abstractions and Microsoft.Extensions.Telemetry.Abstractions had identical names, causing the Telemetry.Abstractions targets to be overridden and preventing proper removal of duplicate source generators.

Key Changes:

  • Renamed MSBuild targets from _Microsoft_Extensions_Logging_AbstractionsRemoveAnalyzers* to _Microsoft_Extensions_Telemetry_AbstractionsRemoveAnalyzers* to prevent name collision
  • Switched from TFM-specific buildTransitive files (net8.0/) to generic buildTransitive files that apply to all target frameworks
  • Added DisableNETStandardCompatErrors=true to prevent automatic TFM-specific buildTransitive folder generation

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/buildTransitive/net8.0/Microsoft.Extensions.Telemetry.Abstractions.targets Deleted TFM-specific targets file in favor of generic approach
src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/buildTransitive/net8.0/Microsoft.Extensions.Telemetry.Abstractions.props Deleted TFM-specific props file in favor of generic approach
src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/buildTransitive/Microsoft.Extensions.Telemetry.Abstractions.targets Created generic targets file with renamed targets to avoid collision
src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/buildTransitive/Microsoft.Extensions.Telemetry.Abstractions.props Created generic props file that applies to all target frameworks
src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Microsoft.Extensions.Telemetry.Abstractions.csproj Updated packaging configuration to use generic buildTransitive files and disable automatic TFM-specific generation

@evgenyfedorov2
Copy link
Member

@joperezr could you review this, please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants