Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.Extensions.Logging
{
internal static class ProviderAliasUtilities
{
private const string AliasAttributeTypeFullName = "Microsoft.Extensions.Logging.ProviderAliasAttribute";
private static readonly string AliasAttributeTypeFullName = typeof(ProviderAliasAttribute).FullName!;
Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Contributor Author

@SetTrend SetTrend Apr 14, 2025

Choose a reason for hiding this comment

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

It's a single call, done only once during the lifetime of an application (static). Do you think this makes much of a difference?

Copy link
Contributor

Choose a reason for hiding this comment

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

I would say yes. This might root the reflection stack on Native AOT.

Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Member

Choose a reason for hiding this comment

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

In other words, this is inconsistent with what we typically do in the rest of the repo.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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 ProviderAliasAttribute

What do you think?

Copy link
Contributor Author

@SetTrend SetTrend Apr 14, 2025

Choose a reason for hiding this comment

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

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just reopened #114606. Please choose.


internal static string? GetAlias(Type providerType)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ public enum LogLevel
Critical = 5,
None = 6,
}
[System.AttributeUsageAttribute(System.AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public partial class ProviderAliasAttribute : System.Attribute
{
public ProviderAliasAttribute(string alias) { }
public string Alias { get { throw null; } }
}
}
namespace Microsoft.Extensions.Logging.Abstractions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
// ------------------------------------------------------------------------------

[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(Microsoft.Extensions.Logging.ILoggingBuilder))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(Microsoft.Extensions.Logging.ProviderAliasAttribute))]
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,4 @@ public static partial class LoggingBuilderExtensions
public static Microsoft.Extensions.Logging.ILoggingBuilder Configure(this Microsoft.Extensions.Logging.ILoggingBuilder builder, System.Action<Microsoft.Extensions.Logging.LoggerFactoryOptions> action) { throw null; }
public static Microsoft.Extensions.Logging.ILoggingBuilder SetMinimumLevel(this Microsoft.Extensions.Logging.ILoggingBuilder builder, Microsoft.Extensions.Logging.LogLevel level) { throw null; }
}
[System.AttributeUsageAttribute(System.AttributeTargets.Class, AllowMultiple=false, Inherited=false)]
public partial class ProviderAliasAttribute : System.Attribute
{
public ProviderAliasAttribute(string alias) { }
public string Alias { get { throw null; } }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
// The .NET Foundation licenses this file to you under the MIT license.

[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(Microsoft.Extensions.Logging.ILoggingBuilder))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(Microsoft.Extensions.Logging.ProviderAliasAttribute))]

This file was deleted.

Loading