MvvmCross Xamarin与SerilogLogProvider的Android链接问题

问题描述 投票:0回答:1

在Xamarin Android中与MvvmCross再次遇到链接器问题。最近升级到MvvmCross 6.1.2并开始使用Serilog。在使用“Sdk和用户程序集”在发布模式下编译时,我在Android安装过程中从base.CreateLogProvider()收到以下错误:

CreateLogProvider()代码:

    public override MvxLogProviderType GetDefaultLogProviderType()
        => MvxLogProviderType.Serilog;

    protected override IMvxLogProvider CreateLogProvider()
    {
        Serilog.Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Error()
            .WriteTo.AndroidLog()
            .CreateLogger();

        return base.CreateLogProvider();
    }

例外:

    System.Exception: HandleAndroidException ---> System.ArgumentNullException: Value cannot be null.
    Parameter name: method
      at System.Linq.Expressions.Expression.Call(System.Linq.Expressions.Expression instance, System.Reflection.MethodInfo method, System.Linq.Expressions.Expression arg0, System.Linq.Expressions.Expression arg1, System.Linq.Expressions.Expression arg2)[0x0009d] in <c674d6912966428cb26998a802347469>:0 
      at System.Linq.Expressions.Expression.Call(System.Linq.Expressions.Expression instance, System.Reflection.MethodInfo method, System.Collections.Generic.IEnumerable`1[T] arguments) [0x00074] in <c674d6912966428cb26998a802347469>:0 
      at System.Linq.Expressions.Expression.Call(System.Linq.Expressions.Expression instance, System.Reflection.MethodInfo method, System.Linq.Expressions.Expression[] arguments) [0x00000] in <c674d6912966428cb26998a802347469>:0 
      at MvvmCross.Logging.LogProviders.SerilogLogProvider.GetForContextMethodCall() [0x00086] in <df54a54dac96467098c3a9eece96a79b>:0 
      at MvvmCross.Logging.LogProviders.SerilogLogProvider..ctor() [0x00018] in <df54a54dac96467098c3a9eece96a79b>:0 
      at MvvmCross.Core.MvxSetup.CreateLogProvider() [0x00045] in <df54a54dac96467098c3a9eece96a79b>:0 
      at myRouteTracker.Droid.Setup.CreateLogProvider() [0x0002a] in <bdd41c7f90344b17a5e6bc21c3959feb>:0 
      at MvvmCross.Core.MvxSetup.InitializeLoggingServices() [0x00000] in <df54a54dac96467098c3a9eece96a79b>:0 
      at MvvmCross.Core.MvxSetup.InitializePrimary() [0x00016] in <df54a54dac96467098c3a9eece96a79b>:0 
      at MvvmCross.Core.MvxSetupSingleton.StartSetupInitialization() [0x0000a] in <df54a54dac96467098c3a9eece96a79b>:0 
      at MvvmCross.Core.MvxSetupSingleton.InitializeAndMonitor(MvvmCross.Core.IMvxSetupMonitor setupMonitor) [0x00042] in <df54a54dac96467098c3a9eece96a79b>:0 
      at MvvmCross.Droid.Support.V7.AppCompat.MvxSplashScreenAppCompatActivity.OnCreate(Android.OS.Bundle bundle) [0x00019] in <2d476ccd46884988a021d78ffc7a2843>:0 
      at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_(System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in <1b4c73c9a2864ea8b5827f31fda08c32>:0 
      at(wrapper dynamic-method) System.Object.e043741f-fba6-4072-a6c2-cfc97949a294(intptr, intptr, intptr)
       --- End of inner exception stack trace ---

对于其他链接问题,我通常会尝试访问LinkerPleaseInclude中被删除的函数。即,在这种情况下,MvvmCross.Logging.LogProviders.SerilogLogProvider.GetForContextMethodCall()。但是,SerilogLogProvider是一个我无法访问的内部类。

我尝试的最后一件事是将Serilog和MvvmCross.Logging.LogProviders添加到“跳过链接程序集”。它既不起作用也不起作用。

非常感谢您的帮助。

问候,

缺口

c# android xamarin.android mvvmcross
1个回答
4
投票

问题是SerilogLogProvider使用反对Serilog.dllSee GitHub SerilogLogProvider source)。即使您可以访问SerilogLogProvider类,也无法阻止链接器剥离所需的方法。原因是链接器实际上正在从Serilog.dll中剥离您需要的方法。

您可以将以下include添加到LinkerPleaseInclude.cs中,以防止链接器剥离SerilogLogProvider使用的方法。

public void Include(Serilog.ILogger logger)
{
    Serilog.Events.LogEventLevel logEventLevel = Serilog.Events.LogEventLevel.Debug;

    _ = Serilog.Log.ForContext("", "", false);
    _ = Serilog.Context.LogContext.PushProperty("", "", false);
    _ = !logger.IsEnabled(logEventLevel);
    logger.Write(logEventLevel, "", new object[0]);
    logger.Write(logEventLevel, new Exception(), "", new object[0]);
}
© www.soinside.com 2019 - 2024. All rights reserved.