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\
.
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");
See Configuration file locations on the NLog wiki.
Basically the ways NLog locates the config is:
There are no other way to do this.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With