-
Notifications
You must be signed in to change notification settings - Fork 752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When transitively including both Microsoft.Extensions.Telemetry.Abstractions and Microsoft.Extensions.Logging.Abstractions, both logging source generators get installed only for .NET 6 and 7 targets #4565
Comments
Tagging subscribers to this area: @dotnet/area-extensions-logging Issue DetailsDescriptionAt least when multi-targeting, if you include one package that transitively includes Microsoft.Extensions.Telemetry.Abstractions and another that transitively includes Microsoft.Extensions.Logging.Abstractions (for 8.0 RC2), both Microsoft.Extensions.Logging.Generators and Microsoft.Gen.Logging are installed for net6.0 and net7.0 targets (but not net462 or net8.0), resulting in two implementations of each partial logging method, causing CS0757 in the generated code and CS0121 where the logging method is called. Reproduction StepsTarget .NET 6 or 7 (or both). Install Microsoft.Extensions.Resilience (to transitively include Microsoft.Extensions.Telemetry.Abstractions) and Microsoft.Extensions.Caching.Memory (to transitively include Microsoft.Extensions.Logging.Abstractions). Create a logging method using the LoggerMessage attribute. Call the logging method. Expected behaviorOnly one implementation of the logging method is generated. Actual behaviorTwo implementations of the logging method are generated. Regression?Never tried this before. Known WorkaroundsHaven't found a workaround. ConfigurationThis is happening building with the 8.0 RC2 SDK on Windows 11 (x64) in Preview 3 of VS 17.8. Other informationNo response
|
Today I installed the latest version of Microsoft.Extensions. Resiliency.Http, so it would be whatever version is included transitively with that. I’ve already put away my computer for the weekend so I can’t check right now (also I uninstalled it to get back to a buildable state before leaving for the weekend, as you probably understand). |
@dariusclay is this issue the same you observed recently? |
I did a quick trial by creating a console app referencing the following packages: <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0-rc.2.23479.6" />
<PackageReference Include="Microsoft.Extensions.Telemetry.Abstractions" Version="8.0.0-rc.2.23510.2" />
</ItemGroup> And I used a simple LoggerMessage like the following: public static partial class Log
{
[LoggerMessage(EventId = 0, Level = LogLevel.Critical, Message = "Could not open socket to `{HostName}`")]
public static partial void CouldNotOpenSocket(ILogger logger, string hostName);
} I found the and here are the final analyzers that are participated in the build But I see another build error:
The complaint is from the line // <auto-generated/>
#nullable enable
#pragma warning disable CS1591 // Compensate for https://github.com/dotnet/roslyn/issues/54103
partial class Log
{
/// <summary>
/// Logs "Could not open socket to `{HostName}`" at "Critical" level.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Gen.Logging", "8.0.0.0")]
public static partial void CouldNotOpenSocket(global::Microsoft.Extensions.Logging.ILogger logger, string hostName)
{
var state = global::Microsoft.Extensions.Logging.LoggerMessageHelper.ThreadLocalState;
_ = state.ReserveTagSpace(2);
state.TagArray[1] = new("HostName", hostName);
state.TagArray[0] = new("{OriginalFormat}", "Could not open socket to `{HostName}`");
logger.Log(
global::Microsoft.Extensions.Logging.LogLevel.Critical,
new(0, nameof(CouldNotOpenSocket)),
state,
null,
static (s, _) =>
{
var HostName = s.TagArray[1].Value ?? "(null)";
return global::System.FormattableString.Invariant($"Could not open socket to `{HostName}`");
});
state.Clear();
}
} @geeknoid may be good if you can have a quick look. |
This trial includes them directly, not transitively. I only saw this behavior when both were included transitively and only for net6.0 and net7.0 targets when multitargeting (but I never tried without multitargeting). |
Yes, I'm also experiencing something similar, thanks @xakep139 In my setup I'm multi-targeting When I take out |
@tarekgh, the |
Can someone that has a repro (@mkane91301 or @dariusclay) attach a repro project so that we can take a look and investigate? It may have to do with the fact that we use |
Yeah, we should do a spot check in dotnet/extensions packages because by default we want no warnings to be emitted inside generated code. Those should all have |
BugRepro.csproj: <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0-rc.2.23479.6" />
<PackageReference Include="Microsoft.Extensions.Resilience" Version="8.0.0-rc.2.23510.2" />
</ItemGroup>
</Project> Program.cs: using Microsoft.Extensions.Logging;
Console.WriteLine("Hello, World!");
internal static partial class LogExtensions
{
[LoggerMessage(1, LogLevel.Information, "Executing action {ActionName}")]
private static partial void LogActionExecuting(ILogger logger, string actionName);
} Note: This worked fine when it only targeted .NET 6. It broke when I switched it to multi-target .NET 6 and 7. |
@joperezr from the build logs, I am seeing the following:
@mkane91301 with the sample project you shared, I am seeing it is always failing either multi-targeting or not. |
I think I found the issue. This happened when we removed the target of (the now unsupported) netcoreapp3.1 in our packages in dotnet/extensions. I'll put up a fix for this and will ask both @tarekgh and @mkane91301 to test against the packages with the fix to ensure this is no longer happening. |
As someone who's been doing this a long time and has seen a lot of bugs, this fix gives me a little chuckle. Good work! |
@mkane91301 in order to close this issue, would you mind testing out locally with package version <?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="dotnet8" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json" />
</packageSources>
</configuration> We would really appreciate your help confirming the issue is fixed 😃 |
It's fixed!!! |
Description
At least when multi-targeting, if you include one package that transitively includes Microsoft.Extensions.Telemetry.Abstractions and another that transitively includes Microsoft.Extensions.Logging.Abstractions (for 8.0 RC2), both Microsoft.Extensions.Logging.Generators and Microsoft.Gen.Logging are installed for net6.0 and net7.0 targets (but not net462 or net8.0), resulting in two implementations of each partial logging method, causing CS0757 in the generated code and CS0121 where the logging method is called.
Reproduction Steps
Target .NET 6 or 7 (or both).
Install Microsoft.Extensions.Resilience (to transitively include Microsoft.Extensions.Telemetry.Abstractions) and Microsoft.Extensions.Caching.Memory (to transitively include Microsoft.Extensions.Logging.Abstractions).
Create a logging method using the LoggerMessage attribute.
Call the logging method.
Expected behavior
Only one implementation of the logging method is generated.
Actual behavior
Two implementations of the logging method are generated.
Regression?
Never tried this before.
Known Workarounds
Haven't found a workaround.
Configuration
This is happening building with the 8.0 RC2 SDK on Windows 11 (x64) in Preview 3 of VS 17.8.
Other information
No response
The text was updated successfully, but these errors were encountered: