Skip to content

Make Microsoft.Extensions.Logging linkable  #40383

@davidfowl

Description

@davidfowl
ILlinker : Unrecognized reflection pattern warning IL2006: Microsoft.Extensions.Logging.ProviderAliasUtilities::GetAlias(System.Type): The return value of method 'System.Type System.Object::GetType()' with dynamically accessed member kinds 'None' is passed into the implicit 'this' parameter of method 'System.Reflection.PropertyInfo System.Type::GetProperty(System.String,System.Reflection.BindingFlags)' which requires dynamically accessed member kinds 'PublicProperties'. To fix this add DynamicallyAccessedMembersAttribute to it and specify at least these member kinds 'PublicProperties'. [C:\dev\git\aspnetcore\src\Hosting\test\testassets\BasicLinkedApp\BasicLinkedApp.csproj]

This is the logic:

https://github.com/dotnet/runtime/blob/6072e4d3a7a2a1493f514cdf4be75a3d56580e84/src/libraries/Common/src/Extensions/ProviderAliasUtilities/ProviderAliasUtilities.cs

Here's some linker friendlier logic:

internal static class ProviderAliasUtilities
{
    private const string AliasAttibuteTypeFullName = "Microsoft.Extensions.Logging.ProviderAliasAttribute";

    internal static string GetAlias(Type providerType)
    {
        foreach (var attributeData in providerType.GetCustomAttributesData())
        {
            if (attributeData.AttributeType.FullName == AliasAttibuteTypeFullName)
            {
                if (attributeData.ConstructorArguments.Count > 0)
                {
                    return attributeData.ConstructorArguments[0].Value?.ToString();
                }
            }
        }

        return null;
    }
}

There are likely other provider specific issues but I haven't encountered those as yet.

cc @eerhardt

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions