'DependencyService.Get<ILogManager>().GetLog() System.NullReferenceException: 'Object reference not set to an instance of an object'

I am trying to execute Xamarin App.

I can build and deploy solutions on both Android and iOS devices. But when I am Debugging/running iOS App I am receiving an error **System.NullReferenceException:** 'Object reference not set to an instance of an object' on
private static ILogger logger = DependencyService.Get<ILogManager>().GetLog(); line

I have installed the latest version of the Nlog NuGet Package.

enter image description here

My ILogManager file is

namespace VolMobile.AppData.Interfaces
{
    public interface ILogManager
    {
        ILogger GetLog([System.Runtime.CompilerServices.CallerFilePath]string callerFilePath = "");

        void Reload();

        void DeleteLog();
    }
}

How can I resolve this issue?

update

My NLogManager iOS file

using System;
using System.Collections.Generic;
using Xamarin.Forms;
using System.IO;
using NLog;
using NLog.Config;
using NLog.Targets;
using VolMobile.AppData.Interfaces;
using VolMobile.iOS.Logging;

[assembly: Dependency(typeof(NLogManager))]
namespace VolMobile.iOS.Logging
{
    public class NLogManager : ILogManager
    {
        string logFile;
        LoggingConfiguration config;


        public NLogManager()
        {

            Reload();

        }



        public void Reload()
        {
            config = new LoggingConfiguration();

            var consoleTarget = new ConsoleTarget();
            config.AddTarget("console", consoleTarget);

            var consoleRule = new LoggingRule("*", LogLevel.Trace, consoleTarget);
            config.LoggingRules.Add(consoleRule);
            //File logging level
            LogLevel llSetting = LogLevel.Off;
            IEnumerable<LogLevel> sysLevels = LogLevel.AllLevels;

            //default to trace at startup
            string currentLogLevel = "Trace";

            //load the app state if available
            if (App.AppState != null)
                currentLogLevel = App.AppState.AppSettings.LogLevel;// AppData.AppData.LogLevel;
            foreach (LogLevel ll in sysLevels)
            {

                if (currentLogLevel == ll.Name)
                {
                    llSetting = ll;
                }
            }

            var fileTarget = new FileTarget();
            string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments); //android.os.environment is the other option
            fileTarget.FileName = Path.Combine(folder, "Log.txt");

            fileTarget.Layout = "${longdate}|${level:uppercase=true}|${callsite}|${appdomain}|${logger}|${threadid}|${message}|{exception:format=tostring}";

            config.AddTarget("file", fileTarget);
            logFile = Path.Combine(folder, "Log.txt");



            var fileRule = new LoggingRule("*", llSetting, fileTarget);//LogLevel.Warn
            config.LoggingRules.Add(fileRule);



            LogManager.Configuration = config;
        }

        public void DeleteLog()
        {
            File.Delete(logFile);
        }

        // Services.Logging.ILogger
        //AppData.Interfaces.ILogger
        //public NLogLogger GetLog([System.Runtime.CompilerServices.CallerFilePath] string callerFilePath = "")
        //{
        //    string fileName = callerFilePath;

        //    if (fileName.Contains("/"))
        //    {
        //        fileName = fileName.Substring(fileName.LastIndexOf("/", StringComparison.CurrentCultureIgnoreCase) + 1);
        //    }

        //    var logger = LogManager.GetLogger(fileName);
        //    return new NLogLogger(logger, logFile);

        //}

        public AppData.Interfaces.ILogger GetLog([System.Runtime.CompilerServices.CallerFilePath] string callerFilePath = "")
        {
            string fileName = callerFilePath;

            if (fileName.Contains("/"))
            {
                fileName = fileName.Substring(fileName.LastIndexOf("/", StringComparison.CurrentCultureIgnoreCase) + 1);
            }

            var logger = LogManager.GetLogger(fileName);
            return new NLogLogger(logger, logFile);
        }
    }
}

The app is working perfectly fine on iOS, Andriod simulators, and Android Device only not on iOS devices.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source