Skip to content Skip to sidebar Skip to footer

Cannot Load Nlog.config In Xamarin

I cannot read nlog.config file in asset folder of android platform NLog.LogManager.Configuration = new XmlLoggingConfiguration('NLog.config'); How to read nlog file and also this

Solution 1:

You can also make use of Xamarin resource. Put the NLog.config file into the library project, then edit file's properties - change the build action to embedded resource.

publicstatic Stream GetEmbeddedResourceStream(Assembly assembly, string resourceFileName)
{
  var resourcePaths = assembly.GetManifestResourceNames()
    .Where(x => x.EndsWith(resourceFileName, StringComparison.OrdinalIgnoreCase))
    .ToList();
  if (resourcePaths.Count == 1)
  {
    return assembly.GetManifestResourceStream(resourcePaths.Single());
  }
  returnnull;
}

var nlogConfigFile = GetEmbeddedResourceStream(myAssembly, "NLog.config");
if (nlogConfigFile != null)
{
    var xmlReader = System.Xml.XmlReader.Create(nlogConfigFile);
    NLog.LogManager.Configuration = new XmlLoggingConfiguration(xmlReader, null);
}

See also: https://github.com/NLog/NLog/wiki/Explicit-NLog-configuration-loading#loading-nlog-configuration-from-xamarin-resource

Solution 2:

you could also try to use this (nlog.config file with a Build Action as an AndroidAsset):

NLog.LogManager.Configuration = new XmlLoggingConfiguration (XmlTextReader.Create(Assets.Open ("NLog.config")), null);

refer to: https://github.com/NLog/NLog/blob/master/src/NLog/Config/LoggingConfigurationFileLoader.cs#L101-L120

Solution 3:

You can add an extension method to your context class that gets you the required asset as a stream:

publicstaticclassUtils
 {
   publicstatic Stream GetFromAssets(this Context context, string assetName)
    {
        AssetManager assetManager = context.Assets;
        Stream inputStream;
        try
        {
            using (inputStream = assetManager.Open(assetName))
            {
                return inputStream;
            }

        }
        catch (Exception e)
        {
            returnnull;
        }
    }
 }

And then in your activity context access it like:

var Asset= context.GetFromAssets("AssetName");

Note that this will return a System.IO.Stream.

Good luck

Revert in case of queries.

Solution 4:

For Xamarin Android "NLog.config" (in this casing) in the assets folder will be loaded automatically. If the file name is different, then use:

LogManager.Configuration = new XmlLoggingConfiguration("assets/someothername.config");

Solution 5:

Thanks for your response. I resolved this issue by setting autoReload="false" throwExceptions="false". Due to these two my config file was not visible. I dont know how they affect the file visibility but setting above two to false i can get config file now Thanks,

Post a Comment for "Cannot Load Nlog.config In Xamarin"