Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make NLog.config file load the file from (d:\dev) instead of "\bin\debug\"

I used Nlog for logging purpose in a particular DLL. The DLL is then used in another application (it is loaded dynamically using System.Reflection.Assembly.LoadFrom(path + a.dll)). I manually placed Nlog.dll and Nlog.config files in Path folder and the application executes properly but it does not log any messages.

However, when I go ahead and place the Nlog.config file manually in application directory (\bin\debug\) is logs messages.

Can someone let me know how to point the search location for Nlog.Config to a different directory (d:\dev) other than \bin\debug\.

like image 663
rookie_developer Avatar asked Apr 11 '13 20:04

rookie_developer


4 Answers

Below is how i changed configuration of Nlog to point to Nlog.config file present in Executing Assembly's folder.

string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(assemblyFolder + "\\NLog.config");
like image 153
rookie_developer Avatar answered Oct 20 '22 08:10

rookie_developer


See Configuration file locations on the NLog wiki.

Basically the ways NLog locates the config is:

  • standard application configuration file (usually applicationname.exe.config)
  • applicationname.exe.nlog in application’s directory
  • NLog.config in application’s directory
  • NLog.dll.nlog in a directory where NLog.dll is located (only if NLog is not in the GAC)
  • file name pointed by the NLOG_GLOBAL_CONFIG_FILE environment variable (if defined, NLog 1.0 only - support removed in NLog 2.0)

There are no other way to do this.

like image 40
Xharze Avatar answered Oct 20 '22 09:10

Xharze


The NLog config needs to reside in the folder where the app that is dynamically pulling a.dll is running from. If you are debugging, that is why it is works when you put it into bin\debug. If you are using Visual Studio, try setting your nlog.config to 'Copy Always' and it should go where you need it.

like image 3
Jim Sowers Avatar answered Oct 20 '22 09:10

Jim Sowers


I found that

NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(logFilePath, true);

actually points the logger to the file which is to be logged to, not the config file. The great thing about this, is that you can define the log file path without needing to know the ExecutingAssembly - this is especially useful when using ExcelDNA etc as the XLL loads the assemblies dynamically as a bitstream, and so

Assembly.GetExecutingAssembly().Location

throws an exception.

like image 3
camt Avatar answered Oct 20 '22 09:10

camt