ProviderAliasAttribute type name reference changed from plain string to type safe expression#114607
ProviderAliasAttribute type name reference changed from plain string to type safe expression#114607SetTrend wants to merge 2 commits intodotnet:mainfrom SetTrend:ProviderAliasAttribute_TypeSafe
ProviderAliasAttribute type name reference changed from plain string to type safe expression#114607Conversation
|
Note regarding the |
1 similar comment
|
Note regarding the |
|
Tagging subscribers to this area: @dotnet/area-extensions-logging |
ProviderAliasAttribute type name reference changed from plain string to type safe expressionProviderAliasAttribute type name reference changed from plain string to type safe expression
…soft.Extensions.Logging.Abstractions` solution.
…g to type safe expression.
|
This LGTM. You may close the other PR #114606. I marked this PR as ready for review too. |
There was a problem hiding this comment.
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Comments suppressed due to low confidence (1)
src/libraries/Microsoft.Extensions.Logging/tests/Common/ProviderAliasAttribute.cs:1
- Removing the ProviderAliasAttribute test implementation may reduce test coverage for alias functionality; please ensure that equivalent tests exist elsewhere.
// Licensed to the .NET Foundation under one or more agreements.
ProviderAliasAttribute type name reference changed from plain string to type safe expressionProviderAliasAttribute type name reference changed from plain string to type safe expression
|
CC @ericstj |
| internal static class ProviderAliasUtilities | ||
| { | ||
| private const string AliasAttributeTypeFullName = "Microsoft.Extensions.Logging.ProviderAliasAttribute"; | ||
| private static readonly string AliasAttributeTypeFullName = typeof(ProviderAliasAttribute).FullName!; |
There was a problem hiding this comment.
This is just an expensive way to compute the constant string. Is it really worth it?
It is very common to hardcode type names as strings. You can find many instances of hardcoded type name strings in this repo.
There was a problem hiding this comment.
It's a single call, done only once during the lifetime of an application (static). Do you think this makes much of a difference?
There was a problem hiding this comment.
I would say yes. This might root the reflection stack on Native AOT.
There was a problem hiding this comment.
Do you think this makes much of a difference?
One-off cases won't make a difference. If we were to use pattern like this everywhere instead of hardcoded strings, it would make a difference.
There was a problem hiding this comment.
In other words, this is inconsistent with what we typically do in the rest of the repo.
There was a problem hiding this comment.
I gave it a second thought: Since ProviderAliasAttribute is statically linked in this project, there won't be any other class definition with this FQ name.
So, it may be logical and coherent to replace the const string comparison
if (attributeData.AttributeType.FullName == AliasAttributeTypeFullName &&in line 22 with an is operator
if (attributeData.AttributeType is ProviderAliasAttributeWhat do you think?
There was a problem hiding this comment.
I forgot to mention that I very much agree with your valuable AOT argument.
I'm not sure if the is operator is made available for all .NET versions.
This particular change I provided just as an option. We can easily refrain from this change and move over to PR #114606. It contains all the changes I made without the string comparison being changed.
| /// Test implementation of ProviderAliasAttribute | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Class)] | ||
| public class ProviderAliasAttribute : Attribute |
There was a problem hiding this comment.
This seems to be used in src/libraries/Microsoft.Extensions.Logging/tests/Common/TestLoggerProvider.cs for testing that the comparison in ProviderAliasUtilities is indeed by name and not by type identity. I think it is better to keep this file.
There was a problem hiding this comment.
@KalleOlaviNiemitalo: I can't find your comment in the "Files changed" tab. Is your comment still valid?
Since the ProviderAliasAttribute moved to Abstractions, it's now available in TestLoggerProvider.cs, so there is no need for duplicate definition. That's why I removed that file.
I found it nice to see that everything falls in place now with the change. No need for a quirk like a duplicate class definition or string comparisons originally implemented as a workaround due to the original class definition not having been available where it should have been.
There was a problem hiding this comment.
Yes it is still valid.
I mean, if some third-party logger provider currently references Microsoft.Extensions.Logging.Abstractions rather than Microsoft.Extensions.Logging, and defines ProviderAliasAttribute as an internal type, then ProviderAliasUtilities should keep recognizing that. And there should be a test to verify that ProviderAliasUtilities does so.
In the existing tests, TestLoggerProvider currently uses the local definition of ProviderAliasAttribute from
src/libraries/Microsoft.Extensions.Logging/tests/Common/ProviderAliasAttribute.cs. If you delete that file, then it will instead use the ProviderAliasAttribute definition that you moved to Microsoft.Extensions.Logging.Abstractions. So this TestLoggerProvider can no longer be used for testing that ProviderAliasUtilities recognizes types by name.
There was a problem hiding this comment.
So, you also plead for keeping the string comparison, like @jkotas does?
No problem. That's why I intentionally created a separate pull request for this. It is just a test drive.
As I wrote above, we can easily refrain from this change and move over to PR #114606. It contains all the changes I made without the string comparison being changed.
There was a problem hiding this comment.
In fact, there are internal definitions of ProviderAliasAttribute in these projects:
https://github.com/DataDog/dd-trace-dotnet/blob/b4342eff1e242dda7b47cc2c90232efa1bf0b221/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Logging/ILogger/DirectSubmission/ProviderAliasAttribute.cs#L18
https://github.com/microsoft/ApplicationInsights-dotnet/blob/a02249a047eb6f97f83e2b733aedc902a7c92363/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Implementation/ProviderAliasAttribute.cs#L9
If you replace the string comparison with a type comparison, it will be a breaking change for those. More breaking than just moving ProviderAliasAttribute from Microsoft.Extensions.Logging to Microsoft.Extensions.Logging.Abstractions.
There was a problem hiding this comment.
I can't find your comment in the "Files changed" tab.
That's because the comment is on a deleted file. If you click "Load diff" above "This file was deleted", then it will show the diff and the comment.
|
Closing this for the favor of #114606 |
resolves #114532