Skip to content
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

Fixing logging generator not getting removed in web projects #4287

Merged
merged 2 commits into from
Aug 16, 2023

Conversation

joperezr
Copy link
Member

@joperezr joperezr commented Aug 14, 2023

Fixes #4286

PR #4238 was meant to automatically remove the platform's logging generator whenever the Microsoft.Extensions.Telemetry package was referenced, which worked just fine for console applications. For web projects though, the issue is that the generator can also be coming from the aspnetcore targeting pack, so we need special logic to be able to remove it from there as well. This PR should address that problem.

Microsoft Reviewers: Open in CodeFlow

@ViktorHofer ViktorHofer requested a review from a team August 14, 2023 19:10
@joperezr
Copy link
Member Author

Saw a failure in CI in the Http.Telemetry tests which I'm almost sure is unrelated, @geeknoid @martintmk did we expect the following test to be flaky?

[xUnit.net 00:00:05.69]     Microsoft.Extensions.Http.Telemetry.Metering.Test.HttpMeteringHandlerTests.When_DiagSourceAndDelegatingHandler_BothConfigured_MetricsOnlyEmittedOnce [FAIL]
[xUnit.net 00:00:05.70]       Assert.Equal() Failure
[xUnit.net 00:00:05.70]       Expected: 1
[xUnit.net 00:00:05.70]       Actual:   2
[xUnit.net 00:00:05.70]       Stack Trace:
[xUnit.net 00:00:05.70]         /_/test/Libraries/Microsoft.Extensions.Http.Telemetry.Tests/Metering/HttpMeteringHandlerTests.cs(1089,0): at Microsoft.Extensions.Http.Telemetry.Metering.Test.HttpMeteringHandlerTests.When_DiagSourceAndDelegatingHandler_BothConfigured_MetricsOnlyEmittedOnce()
[xUnit.net 00:00:05.70]         --- End of stack trace from previous location ---
  Passed Microsoft.Extensions.Http.Telemetry.Metering.Test.HttpMeteringHandlerTests.AddHttpClientMeteringForAllHttpClients_EmitMetrics_OnException [2 s]
  Passed Microsoft.Extensions.Http.Telemetry.Tracing.Test.HttpClientTracingExtensionsTests.AddHttpClientTracing_GetRequestMetadataFromDownstreamDependencyMetadataManager_ActivityEnrichedWithMetadata [1 s]
  Failed Microsoft.Extensions.Http.Telemetry.Metering.Test.HttpMeteringHandlerTests.When_DiagSourceAndDelegatingHandler_BothConfigured_MetricsOnlyEmittedOnce [135 ms]
  Error Message:
   Assert.Equal() Failure
Expected: 1
Actual:   2
  Stack Trace:
     at Microsoft.Extensions.Http.Telemetry.Metering.Test.HttpMeteringHandlerTests.When_DiagSourceAndDelegatingHandler_BothConfigured_MetricsOnlyEmittedOnce() in /_/test/Libraries/Microsoft.Extensions.Http.Telemetry.Tests/Metering/HttpMeteringHandlerTests.cs:line 1089
--- End of stack trace from previous location ---

If it's just a one-off failure I guess it's fine ignoring, but if we see that again or have seen it before we should quarantine the test so we can investigate.

@xakep139
Copy link
Member

@joperezr you can safely ignore that failure as we decided to remove metering bits from the repo anyway: #4214

Copy link
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

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

Other than the comment I left, LGTM!

@RussKie
Copy link
Member

RussKie commented Aug 16, 2023

Please re-target to release/8.0, if this is necessary for .NET 8.

@joperezr joperezr changed the base branch from main to release/8.0 August 16, 2023 19:31
@joperezr joperezr enabled auto-merge (squash) August 16, 2023 20:12
@joperezr joperezr merged commit 171c0df into release/8.0 Aug 16, 2023
6 checks passed
@joperezr joperezr deleted the joperezr/FixWebProjectsLogging branch August 16, 2023 22:37
@joperezr
Copy link
Member Author

/backport to release/8.0-rc1

@github-actions
Copy link
Contributor

Started backporting to release/8.0-rc1: https://github.com/dotnet/extensions/actions/runs/5906347957

@RussKie RussKie added this to the 8.0 RC2 milestone Aug 28, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Sep 27, 2023
@@ -1,2 +1,16 @@
<Project>
<!-- This package should replace the Microsoft.Extensions.Logging.Abstractions source generator. -->
<Target Name="_Microsoft_Extensions_Logging_AbstractionsRemoveAnalyzers"
Copy link
Member

Choose a reason for hiding this comment

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

I'm curious, why did we choose to reuse the target name and didn't create a new one (e.g., _Microsoft_Extensions_Logging_AbstractionsRemoveAnalyzersForSure)? I wonder if MSBuild handles targets with the same name correctly.

Copy link
Member

Choose a reason for hiding this comment

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

Targets with the same name are overwritten. The last one wins.

Copy link
Member Author

Choose a reason for hiding this comment

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

Exactly, and IIRC in this particular case it was by design and intentional to replace the other target as we wanted that the same property would trigger this target without the need of hooking it up again.

Overwriting targets is pretty common in MSBuild, that is for instance how cross-compiling works when doing the outer-builds (By re-defining the Build, Test, Restore, etc targets for outer builds.)

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, what guarantees that this target is imported after the Microsoft.Extensions.Logging.Abstractions target when referencing the packages?

Copy link
Member Author

Choose a reason for hiding this comment

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

Great question, I don't think that is deterministic. I did this a while back so don't recall exactly (and clearly I should've added better docs for it :P) but perhaps the issue then was that there were few cases where this target wouldn't be defined, so for those cases I'm just ensuring it's defined.

Copy link
Member Author

@joperezr joperezr Dec 2, 2023

Choose a reason for hiding this comment

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

Ohh ohh I think I remember now 😄. The logging generator is part of the aspnetcore targeting pack, so aspnetcore projects wouldn't need reference to the logging.abstractions package, and therefore wouldn't import the buildTransitive targets that disable the package. For these cases, I'm providing the target in our package as well, in order to make sure that the generator is also removed for aspnetcore apps.

We could have of course picked a different target name, but then we would have to add logic to detect if we are NOT an aspnetcore app and getting the reference from the package instead, so that we don't try to run the same operation twice. By just re-defining the target, we have the same mechanism for removing the generator whether your application references the logging.generator package, or the aspnetcore targeting pack, and we are guaranteed to only run once.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, that seems to be the reason why we picked this approach. If you read into the comments of the issue that this PR resolved, you can see how we realized about this case and picked this design to fix it: #4286 (comment)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Logging Source Generator emits CS0757 errors when referencing Microsoft.Extensions.Telemetry
6 participants