简体   繁体   English

C#Mono NLog部署到Raspberry PI时无法加载文件或程序集

[英]C# Mono NLog Could not load file or assembly When Deployed to Raspberry PI

I have just begun messing with C# on my Raspberry Pi and it seems to be going well. 我刚刚开始在Raspberry Pi上使用C#搞乱,看来一切进展顺利。 I am developing my C# application on my Arch Linux machine with Monodevelop and scp-ing everything in the bin/Debug/ directory to my Raspberry Pi and then running "mono my.exe". 我正在用Monodevelop在Arch Linux机器上开发C#应用程序,然后将bin / Debug /目录中的所有内容都剪切到Raspberry Pi中,然后运行“ mono my.exe”。 This has worked with some simple applications I have played with but I am having trouble with NLog. 这已与我玩过的一些简单应用程序一起使用,但是我在使用NLog时遇到了麻烦。 The executable runs fine on my development machine but I get this exception on the Raspberry Pi: 可执行文件可以在我的开发机器上正常运行,但是在Raspberry Pi上却遇到此异常:

mono NLogTest.exe 

Unhandled Exception:
System.TypeInitializationException: The type initializer for 'NLogTest.MainClass' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly or one of its dependencies.
  at NLog.LogFactory.get_Configuration () [0x000aa] in <eb9f75c9ffa040549f1fea1320a5bf6c>:0 
  at NLog.LogFactory.GetLogger (NLog.LogFactory+LoggerCacheKey cacheKey) [0x0012c] in <eb9f75c9ffa040549f1fea1320a5bf6c>:0 
  at NLog.LogFactory.GetLogger (System.String name) [0x00011] in <eb9f75c9ffa040549f1fea1320a5bf6c>:0 
  at NLog.LogManager.GetCurrentClassLogger () [0x0000a] in <eb9f75c9ffa040549f1fea1320a5bf6c>:0 
  at NLogTest.MainClass..cctor () [0x00000] in <689b8644ac61434f9d79427c3bf696a7>:0 
   --- End of inner exception stack trace ---

It seems as if NLog can't find the config file but it is in the same directory. 似乎NLog找不到配置文件,但它位于同一目录中。

The simple example I am using is: 我使用的简单示例是:

using System;
using NLog;
namespace NLogTest
{
    class MainClass
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        public static void Main(string[] args)
        {
            Console.WriteLine("Testing NLog");
            logger.Trace("A Trace!!");
            logger.Debug("Sample debug message");
            logger.Info("Sample informational message");
            logger.Warn("Sample warning message");
            logger.Error("Sample error message");
            logger.Fatal("Sample fatal error message");
            Console.Write("DONE LOGGING!");
        }
    }
}

and my NLog.config file is pretty simple: 我的NLog.config文件非常简单:

<?xml version="1.0" encoding="UTF-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      throwExceptions="false">
    <targets>
        <target xsi:type="File"
            name="logFile"            
            fileName="The.log"
            />
    </targets>
    <rules>
        <logger name="*" writeTo="logFile" minlevel="Info" />
    </rules>
</nlog>

The directory on my Raspberry Pi contains the exe, the NLog.config file, and the NLog.dll. 我的Raspberry Pi上的目录包含exe,NLog.config文件和NLog.dll。

I installed the mono-runtime package from apt on the Raspberry pi. 我在Raspberry pi上从apt安装了单运行时软件包。 On my development machine, the Monodevelop build options for the project are set to target "Mono/.NET 4.5". 在我的开发计算机上,该项目的Monodevelop构建选项设置为目标“ Mono / .NET 4.5”。 I didn't think it would matter that I am developing on an x86 machine and deploying to an ARM-A machine because I thought the mono runtime would handle the architecture differences. 我认为在x86机器上进行开发并部署到ARM-A机器并不重要,因为我认为mono runtime可以处理架构差异。

Any help here would be appreciated, thanks. 谢谢您在这里的任何帮助。

Think the problem is that NLog was born in the Windows world with case-insensitive filenames. 认为问题在于NLog诞生于Windows世界,其文件名不区分大小写。 Linux is case sensitive. Linux区分大小写。

You have to specify the path to the nlog.config manually (with the right case): 您必须手动指定nlog.config的路径(使用正确的大小写):

LogManager.Configuration = new XmlLoggingConfiguration("path/nlog.config");

You have to delay the call to LogManager.GetCurrentClassLogger() until having loaded the configuration manually. 您必须延迟对LogManager.GetCurrentClassLogger()的调用,直到手动加载配置为止。

Thanks to @Rolf Kristensen, I moved the LogManager.GetCurrentClassLogger call into main. 感谢@Rolf Kristensen,我将LogManager.GetCurrentClassLogger调用移到了main中。 Then I got the message 然后我得到消息

Can't find custom attr constructor image: /home/pi/monotest/NLog.dll mtoken: 0x0a00001f due to Could not load file or assembly or one of its dependencies. 找不到自定义attr构造函数图像:/home/pi/monotest/NLog.dll mtoken:0x0a00001f由于无法加载文件或程序集或其依赖项之一。 assembly:System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 type: member: 程序集:System.Core,版本= 4.0.0.0,区域性=中性,PublicKeyToken = b77a5c561934e089类型:成员:

This led me to this github issue: https://github.com/NLog/NLog/issues/905 这使我想到了这个github问题: https : //github.com/NLog/NLog/issues/905

Where if I installed libmono-system-servicemodel4.0a-cil onto the target raspberry pi with apt-get , then it works! 如果我使用apt-getlibmono-system-servicemodel4.0a-cil安装到目标树莓apt-get ,那么它将起作用!

Also this is with the target framework as .NET 4.5 and the Target Architecture as "Any". 目标框架为.NET 4.5,目标体系结构为“ Any”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM