Rico227
Rico227

Reputation: 62

NLog with Xamarin cross-plattform application doesn't log anything

I set up NLog like this in the Android part of my Xamarin-project.

My NLog.config looks like this:

<?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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="true"
      throwConfigExceptions="true"
      internalLogLevel ="Trace"
      internalLogFile ="C:\Users\MyUser\intLogFile.txt"
      internalLogToConsole="true"
      internalLogIncludeTimestamp ="true">
     <targets>
        <target name="file" xsi:type="File"
            layout="${longdate} ${logger} ${message}${exception:format=ToString}" 
            fileName="/storage/emulated/0/Download/${shortdate}.log" />
    </targets>
 
    <rules>
        <logger name="*" minlevel="Debug" writeTo="file" />
    </rules>
</nlog>

Has somebody an idea why NLog is still not logging anything?

--------------------- UPDATE ----------------------------

NLog is now logging with basic setup of a LogService-class:

 public void Initialize(Assembly assembly, string assemblyName)
    {
       
        var location = $"{assemblyName}.NLog.config";
        var stream = assembly.GetManifestResourceStream(location);
        NLog.LogManager.Configuration = new XmlLoggingConfiguration(XmlReader.Create(stream), null);

        NLog.Common.InternalLogger.LogFile = "C:\\Users\\MyUser\\Documents\\internalLog.log";

        NLog.Common.InternalLogger.LogWriter = new StringWriter();

        NLog.Common.InternalLogger.LogLevel = LogLevel.Debug;

    }

But the internal log is still not showing up. As you can see I tried configuring the InternalLogger in code and also in the NLog.config. I tried putting the NLog.config in the assets-folder and in the main android-project and I set build action to respectively embedded ressource or android asset.

--------------------- 2nd UPDATE --------------------------

The InternalLogger is now working on my Android-device. In this tutorial the internal file is logged to c://temp/... which I assume is a path on Windows. But I am still not able to achieve this during debugging. Is this even possible?

Upvotes: 1

Views: 878

Answers (1)

Rico227
Rico227

Reputation: 62

Following some additional troubleshooting tips that helped me solving my issue:

  • If you are using a real Andriod-device and not an emulator unplug the debugging connection and reconnect it. The Windows-File-Explorer
    sometimes is not updating the files in the folders of the device if
    it is not reconnected.

  • Basic initializing in the code should be enough:

       public void Initialize(Assembly assembly, string assemblyName)
       {
    
               var location = $"{assemblyName}.NLog.config";
               var stream = assembly.GetManifestResourceStream(location);
               NLog.LogManager.Configuration = new XmlLoggingConfiguration(XmlReader.Create(stream), null);
       }
    
  • You should be fine by putting NLog.config as an embedded resource in the main folder of the android project. If not put it as an android asset in the assets-folder.

  • If it is still not logging try to set the configuration up programmatically

  • Also make sure your minlevel in the .config is lower than the loglevel you are using.

Upvotes: 1

Related Questions