Home C# Mono NLog Could not load file or assembly When Deployed to Raspberry PI
Reply: 2

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

Jesse
1#
Jesse Published in 2017-11-15 01:23:33Z

I have just begun messing with C# on my Raspberry Pi and it seems to be going well. 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". This has worked with some simple applications I have played with but I am having trouble with NLog. The executable runs fine on my development machine but I get this exception on the 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.

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:

<?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.

I installed the mono-runtime package from apt on the Raspberry pi. On my development machine, the Monodevelop build options for the project are set to target "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.

Any help here would be appreciated, thanks.

Rolf Kristensen
2#
Rolf Kristensen Reply to 2017-11-15 18:44:46Z

Think the problem is that NLog was born in the Windows world with case-insensitive filenames. Linux is case sensitive.

You have to specify the path to the nlog.config manually (with the right case):

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

You have to delay the call to LogManager.GetCurrentClassLogger() until having loaded the configuration manually.

Jesse
3#
Jesse Reply to 2017-11-16 02:17:05Z

Thanks to @Rolf Kristensen, I moved the LogManager.GetCurrentClassLogger call into 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. assembly:System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 type: member:

This led me to this github issue: 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!

Also this is with the target framework as .NET 4.5 and the Target Architecture as "Any".

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.389889 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO