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

Native AOT warnings #5258

Closed
philfontaine opened this issue Jun 17, 2023 · 5 comments
Closed

Native AOT warnings #5258

philfontaine opened this issue Jun 17, 2023 · 5 comments

Comments

@philfontaine
Copy link

philfontaine commented Jun 17, 2023

NLog 5.2 just got released, which removes trim warnings.

Therefore, all issues related to that got closed, namely #5198 and #3777.

However, compiling for Native AOT still gives AOT warnings. Is this a separate topic, or was that intended to be fixed in 5.2 as well?

Here's a screenshot of a very simple reproduction. I can put the repro on github if needed.

image

NLog version: 5.2.0

Platform: .NET 7

@welcome
Copy link

welcome bot commented Jun 17, 2023

Hi! Thanks for opening your first issue here! Please make sure to follow the issue template - so we could help you better!

@snakefoot
Copy link
Contributor

snakefoot commented Jun 17, 2023

NLog uses compiled expression trees to optimize speed for doing object-reflection of custom logging-objects. Ex:

var v = new { Amount = 108, Message = "Hello" };
_logger.Info("{@Hello}", v);  // Uses reflection

I'm guessing that is also why your build also warns about System.Linq.Expressions is part of the build, because it is referenced by NLog. See also Improve performance with Expression Trees

  • Object Property Reflection - Should be extracted to separate-nuget-package
  • Dynamic Filtering - Should be changed to direct method call by default (See also ConditionMethodExpression - Without reflection by default #5260)
  • Method Call Target - Should be split into MethodCallTarget + MethodCallDynamicTarget (separate nuget-package)

When reading about PublishAOT then it says:

  • Requires trimming
  • No dynamic loading (for example, Assembly.LoadFile).
  • No run-time code generation (for example, System.Reflection.Emit).
  • System.Linq.Expressions always use their interpreted form

When using PublishAOT then there is no JIT-compiler available for optimizing Expression-Trees, and I'm guessing that is why that warnings are generated.

I'm not sure there is anyway to work-around this, besides waiting for NLog v6 that hopefully will extract the use of Expression-Trees into a separate nuget-package. Or maybe Microsoft will optimize their support for Expression-Trees in NET8 / NET9 so it no longer gives build-warnings. Until that happens, then you have to live with using PublishReadyToRun

If you find places where NLog can be optimized in regard to trimming / ready-to-run / AOT, then pull-requests are most wellcome.

@snakefoot
Copy link
Contributor

snakefoot commented Jun 17, 2023

Getting the following warnings from NLog TestTrimPublish-example-project when using <PublishAot>true</PublishAot> with <TargetFramework>net7.0</TargetFramework>:

3>"C:\Users\snakefoot\.nuget\packages\runtime.win-x64.microsoft.dotnet.ilcompiler\7.0.5\tools\\ilc" @"obj\Release\net7.0\win-x64\native\TestTrimPublish.ilc.rsp"
3>NLog.Layouts.LayoutParser.ParseLayoutRendererPropertyValue(ConfigurationItemFactory,SimpleStringReader,Nullable`1<Boolean>,String,PropertyInfo): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime.
3>NLog.Layouts.ValueTypeLayoutInfo.CreateTypedLayout(Type,Object): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime.
3>NLog.Internal.ObjectReflectionCache.TryLookupExpandoObject(Object,ObjectPropertyList&): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime.
3>NLog.Internal.PropertyHelper.TryNLogSpecificConversion(Type,String,ConfigurationItemFactory,Object&): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime.
3>NLog.Internal.PropertyHelper.CreateCollectionHashSetInstance(Type,Object,MethodInfo&): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime.
3>NLog.Internal.PropertyHelper.CreateCollectionHashSetInstance(Type,Object,MethodInfo&): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime.
3>NLog.Internal.PropertyHelper.CreateCollectionListInstance(Type,MethodInfo&): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime.
3>NLog.Internal.PropertyHelper.CreateCollectionListInstance(Type,MethodInfo&): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime.
3>NLog.Internal.PropertyHelper.ExtractHashSetComparer(Object,PropertyInfo): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime.
3>Assembly 'System.Linq.Expressions' produced AOT analysis warnings.
3>Assembly 'System.Private.Xml' produced AOT analysis warnings.

Looks like ValueTypeLayoutInfo should be changed to not use typed Layout<T>. See also #5262

I guess all the other location that uses MakeGenericType to assign properties can have their warning suppressed, since the PropertyInto.PropertyType ensures the PropertyType is available for AOT.

Also interesting that System.Xml is also giving issues. Guess the ability to load NLog.config using System.Xml should not be included in NLog-core.

Also interesting that System.Linq.Expressions is giving issues. Guess Expression-Tree-Compiling must be extracted.

@snakefoot
Copy link
Contributor

Closing as duplicate of #3366

@snakefoot snakefoot added this to the 6.0 milestone Jun 24, 2023
@snakefoot
Copy link
Contributor

snakefoot commented Sep 28, 2023

Current work-around is probably specifying RdXmlFile in csproj-project-file, and adding NLog to rd.xml:

<Project>
  <ItemGroup>
      <RdXmlFile Include="rd.xml" />
  </ItemGroup>

</Project>

rd.xml:

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
        <!-- .NET Native fix -->
        <Assembly Dynamic="Required All" Name="NLog"/>
  </Application>
</Directives>

But unsure if it will remove warnings.

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

No branches or pull requests

2 participants