You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
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
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.
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).
And I used a simple LoggerMessage like the following:
publicstaticpartialclassLog{[LoggerMessage(EventId=0,Level=LogLevel.Critical,Message="Could not open socket to `{HostName}`")]publicstaticpartialvoidCouldNotOpenSocket(ILoggerlogger,stringhostName);}
I found the Microsoft.Extensions.Logging.Generators is successfully removed from the compilation as expected:
and here are the final analyzers that are participated in the build
But I see another build error:
D:\Temp\LogSourceGenRepro\Microsoft.Gen.Logging\Microsoft.Gen.Logging.LoggingGenerator\Logging.g.cs(13,21): error EXTEXP0003: 'Microsoft.Extensions.Logging.LoggerMessageHelper' is for evaluation purposes only and is subject to change or removal in future updates
. Suppress this diagnostic to proceed. (https://aka.ms/dotnet-extensions-warnings/EXTEXP0003) [D:\Temp\LogSourceGenRepro\LogSourceGenRepro.csproj]
The complaint is from the line var state = global::Microsoft.Extensions.Logging.LoggerMessageHelper.ThreadLocalState; in the generated code is:
// <auto-generated/>
#nullable enable
#pragma warning disable CS1591// Compensate for https://github.com/dotnet/roslyn/issues/54103partialclassLog{/// <summary>/// Logs "Could not open socket to `{HostName}`" at "Critical" level./// </summary>[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Gen.Logging","8.0.0.0")]publicstaticpartialvoidCouldNotOpenSocket(global::Microsoft.Extensions.Logging.ILoggerlogger,stringhostName){varstate=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,_)=>{varHostName=s.TagArray[1].Value??"(null)";returnglobal::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).
@tarekgh, the [Experimental] attribute is reported as an error as described in dotnet/csharplang#7597 and it can be suppressed with #pragma warning disable EXTEXP0003 in that case. The generated code can't be modified though, but the [Experimental] attribute was removed from LoggerMessageHelper. Thus, it shouldn't emit the diagnostic in 8.0 release.
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 build\ folder inside the package as opposed to buildTransitive\, but in order to validate it I want to have a repro project to test the fix.
@tarekgh, the [Experimental] attribute is reported as an error as described in dotnet/csharplang#7597 and it can be suppressed with #pragma warning disable EXTEXP0003 in that case. The generated code can't be modified though, but the [Experimental] attribute was removed from LoggerMessageHelper. Thus, it shouldn't emit the diagnostic in 8.0 release.
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 #pragma to make sure they don't throw a warning, since as you said, this can't be fixed unless we use NoWarn and that shouldn't be the case. Instead we should just make sure that the gesture that triggers the generator (the attribute in this case) would throw the warning.
@joperezr from the build logs, I am seeing the following:
SupportsRoslynComponentVersioning = true
Target "_Microsoft_Extensions_Logging_AbstractionsAnalyzerMultiTargeting" skipped, due to false condition; ('$(SupportsRoslynComponentVersioning)' != 'true') was evaluated as ('true' != 'true').
Target "_Microsoft_Extensions_Logging_AbstractionsRemoveAnalyzers" skipped, due to false condition; ('$(DisableMicrosoftExtensionsLoggingSourceGenerator)' == 'true') was evaluated as ('' == 'true').
DisableMicrosoftExtensionsLoggingSourceGenerator is not defined at all.
@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.
@mkane91301 in order to close this issue, would you mind testing out locally with package version 8.0.0-rtm.23516.7 of Microsoft.Extensions.Resilience? No need to change the version of the other package. In order for restore to work, you will need to add a NuGet.config right next to your .csproj with contents like this:
Activity
tarekgh commentedon Oct 13, 2023
What is the package version did you use for
Microsoft.Extensions.Telemetry.Abstractions? I think @joperezr already fixed that.CC @joperezr @geeknoid
mkane91301 commentedon Oct 14, 2023
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).
xakep139 commentedon Oct 15, 2023
@dariusclay is this issue the same you observed recently?
tarekgh commentedon Oct 16, 2023
I did a quick trial by creating a console app referencing the following packages:
And I used a simple LoggerMessage like the following:
I found the
Microsoft.Extensions.Logging.Generatorsis successfully removed from the compilation as expected:and here are the final analyzers that are participated in the build
But I see another build error:
The complaint is from the line
var state = global::Microsoft.Extensions.Logging.LoggerMessageHelper.ThreadLocalState;in the generated code is:@geeknoid may be good if you can have a quick look.
mkane91301 commentedon Oct 16, 2023
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).
dariusclay commentedon Oct 16, 2023
Yes, I'm also experiencing something similar, thanks @xakep139
In my setup I'm multi-targeting
net462,net6.0andnet7.0, using the same versions as the test @tarekgh has.When I take out
Microsoft.Extensions.Telemetry.Abstractionspackage, the logging source generator works as intended. This is on VS 17.7.5.xakep139 commentedon Oct 16, 2023
@tarekgh, the
[Experimental]attribute is reported as an error as described in dotnet/csharplang#7597 and it can be suppressed with#pragma warning disable EXTEXP0003in that case. The generated code can't be modified though, but the[Experimental]attribute was removed fromLoggerMessageHelper. Thus, it shouldn't emit the diagnostic in 8.0 release.joperezr commentedon Oct 16, 2023
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
build\folder inside the package as opposed tobuildTransitive\, but in order to validate it I want to have a repro project to test the fix.joperezr commentedon Oct 16, 2023
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
#pragmato make sure they don't throw a warning, since as you said, this can't be fixed unless we use NoWarn and that shouldn't be the case. Instead we should just make sure that the gesture that triggers the generator (the attribute in this case) would throw the warning.mkane91301 commentedon Oct 16, 2023
BugRepro.csproj:
Program.cs:
Note: This worked fine when it only targeted .NET 6. It broke when I switched it to multi-target .NET 6 and 7.
tarekgh commentedon Oct 16, 2023
@joperezr from the build logs, I am seeing the following:
DisableMicrosoftExtensionsLoggingSourceGeneratoris not defined at all.@mkane91301 with the sample project you shared, I am seeing it is always failing either multi-targeting or not.
joperezr commentedon Oct 16, 2023
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.
mkane91301 commentedon Oct 17, 2023
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!
joperezr commentedon Oct 17, 2023
@mkane91301 in order to close this issue, would you mind testing out locally with package version
8.0.0-rtm.23516.7of Microsoft.Extensions.Resilience? No need to change the version of the other package. In order for restore to work, you will need to add a NuGet.config right next to your .csproj with contents like this:We would really appreciate your help confirming the issue is fixed 😃
mkane91301 commentedon Oct 17, 2023
It's fixed!!!