Reading NLog.config from NLog in API REST .NET


I have a problem reading the configuration file (NLog.config) from my class library project when I call it from my ASP.NET MVC4 project.

I tested my code using an Unit Test Project and reads the configuration file properly and makes logging correctly on the file that I specified on the NLog.config, so the NLog.config is correctly defined.

I also set "Copy always" at Copy to Output Directory settings, just like the tutorial says.

The problem is that when I called the class library project, from my ASP project, I realized that wasn't making any kind of log, and when I debugged, I saw that It wasn't picking the configuration from NLog.config, and for that reason, my project didn't know when and where to log.

When I looked into "ASP project folder"/bin, there are the files from NLog: NLog.dll, NLog.config and NLog.xml, so in theory, the ASP project, can access without problem to the NLog files.

I read the tutorial from NLog but I can't figured out the solution. Just can´t understand why from my test project works properly but when I make the same call from my ASP project just don't work.

This is my configuration file for NLog.

<?xml version="1.0" encoding="utf-8" ?> <nlog throwExceptions="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <targets> <target name="logfile" xsi:type="File" fileName="C:\logfile.txt" layout="${callsite:fileName=true} - ${longdate} - ${level:uppercase=true} - ${logger} - ${message}"/> </targets> <rules> <logger name="*" minlevel="Info" writeTo="logfile" /> </rules> </nlog>

And within my classes, I create the logger this way:

private static Logger logger = LogManager.GetCurrentClassLogger();

Can anybody help me with this problem?


Try turning on <a href="http://nlog-project.org/wiki/Internal_logging" rel="nofollow">internal logging</a> of nlog itself to see what it might tell you.

<nlog internalLogFile="c:\log.txt" internalLogLevel="Trace">

You might find it's a rights issue.


I figured out the solution.

Seems like with the copy to output directory setting, isn't enough with asp.net-mvc4.

<a href="http://img820.imageshack.us/img820/23/sol2c.png" rel="nofollow">Copy to output directory http://img820.imageshack.us/img820/23/sol2c.png</a>

I had to manually copy the NLog.config into the asp project itself inside the Solution explorer.

<a href="http://imageshack.us/a/img849/2460/sol1o.png" rel="nofollow">Project explorer http://imageshack.us/a/img849/2460/sol1o.png</a>

In this example, I copied the NLog.config from LoggingLibrary into LogWebApp project and It works well.



  • Gatttool non-interactive mode --char-write
  • how to build game playing neural network in Python?
  • Are there algorithms for putting a digest into the file being digested?
  • Available space left on drive - WinAPI - Windows CE
  • Translating C# to PowerShell in InterIMAP
  • NHibernate manually control fetching
  • nodemcu custom firmware build problems
  • Android Studio Can't Find tools.jar
  • Convert SQLite database to XML
  • Eclipse CDT error: Unable to compile
  • Xcode 4 NSLog Macro link in Xcode 3
  • Very simple C++ DLL that can be called from .net
  • Play WS (2.2.1): post/put large request
  • Yii2: Config params vs. const/define
  • Ajax Loaded meta Tags
  • Paperclip, set path outside of rails root folder
  • ilmerge with a PFX file
  • Deserializing XML into class C#
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Upload files with Ajax and Jquery
  • Apache 2.4 - remove | delete | uninstall
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • How do I rollback to a specific git commit
  • Arrays break string types in Julia
  • Is there a mandatory requirement to switch app.yaml?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Proper folder structure for lots of source files
  • SQL merge duplicate rows and join values that are different
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Proper way to use connect-multiparty with express.js?
  • Benchmarking RAM performance - UWP and C#
  • Load html files in TinyMce
  • LevelDB C iterator
  • Can't mass-assign protected attributes when import data from csv file
  • Append folder name and increment by 1 using batch script
  • Busy indicator not showing up in wpf window [duplicate]
  • How to Embed XSL into XML