سؤال

I just can't make NLog to work reliably under Windows Server 2012/IIS 8. This is an Azure Virtual Machine (not a webrole) so things should have been pretty simple. Originally I had acync wrappers, database-based targets, etc. But I've stripped everything down to the bare mininum and can't seem to make it work!

This is my NLog.config file:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      >

    <targets>
        <target xsi:type="File"
             name="nsec"
             fileName="${basedir}app_data/logs/nsec_${shortdate}.log"
             createDirs="true"
             layout="${longdate}|${level:uppercase=true}|${logger}|${aspnet-user-identity}|${message}"
             />
    </targets>

  <rules>
    <logger name="NSec" minlevel="Trace" writeTo="nsec"/> 
  </rules>
</nlog>

As you can see, there's nothing fancy here. The file target should have written to the ./App_Data/Logs directory, but most of the time nothing got written there. Sometimes something gets written to the file... And that's the most infuriating part of it all!

Locally under both Cassini and IIS Express the logs work perfectly.

Is there anything I need to add to my web.config for NLog to work? I'm not even using the ASP.NET wrapper!

Does anyone know of any incompatibility between NLog and IIS8?

UPDATE

I've run Process Monitor on the server and realized not even a single attempt is made to write, read, or query the log file. It looks as if NLog was being completely and utterly ignored. I know the NLog.config file is being read at startup because if I put an invalid configuration I get an error when accessing my application, so NLog is parsing the file. The problem is that even though it is being parsed, and sometimes something gets logged, most of the time NOTHING happens, and, as confirmed by using Process Monitor, not a single attempt is being made to access/create/write the log file.

The irony is that on Process Monitor I could see the other IIS Applications writing to their own NLog files without issues. It's only this specific application that is failing to log, but only in the production machine!

هل كانت مفيدة؟

المحلول

I've just discovered, with the help of Uwe Kein, who suggested me to use Process Monitor to see if there was any problems with permissions that, in fact, the log file was NEVER even accessed. This started me wondering why... And I just realized that the HttpModule which was the source of the NSec logger was not running at all, so, in fact, no log was actually ever being made.

This problem occurred because the web.config on the destination machine wasn't successfully updated in previous deployments, so the HttpModule was configured the IIS6 way... I had to add it to the modules section as shown in: https://stackoverflow.com/a/2935410/285678

What is great about it was that I've just added this log to try to find the reason why this HttpModule was not behaving as expected, so finding out why NLog was "not working" resolved this issue (which was a very serious issue, as the HttpModule pertained to the security system of the application!)

I've tried to close the question since it is misleading, as it is not an NLog error at all, but it needs 50 votes to be closed! So I'll left it as a warning to people like me who, in desperation, ask "not very wise" questions in StackOverflow :-(

نصائح أخرى

I had put a blank Nlog config section in the web.config file, and that made it not look for the nlog.config file. Once I deleted that, my logging started working.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top