问题
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?
回答1:
I figured out the solution.
Seems like with the copy to output directory setting, isn't enough with asp.net-mvc4.
I had to manually copy the NLog.config into the asp project itself inside the Solution explorer.
Project explorer http://imageshack.us/a/img849/2460/sol1o.png
In this example, I copied the NLog.config from LoggingLibrary into LogWebApp project and It works well.
Greetings!
回答2:
Try turning on internal logging 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.
来源:https://stackoverflow.com/questions/13469474/reading-nlog-config-from-nlog-in-api-rest-net