3

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!

Loudenvier
  • 8,362
  • 6
  • 45
  • 66
  • 2
    How about running [Process Monitor](http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx) on the server and see whether there are any permissions missing on the target folders? – Uwe Keim Nov 12 '13 at 19:48
  • 1
    Hi Uwe, this is a production server.. I don't want to run Process Monitor there... But I can assure that the IIS App Domain has rights to all folders as it runs with a specific user identity. If I can't get it to work, I'll try to duplicate the Virtual Machine and use Process Monitor there, but Azure doesn't make it easy to duplicate VMs, in fact it is, most of the times, harder than install one from stratch! – Loudenvier Nov 12 '13 at 19:58
  • 1
    Uwe, i've used Process Monitor and there's not even a single attempt to write to the log file!!! It looks as if NLog was being completely and utterly ignored. I'm updating the question... – Loudenvier Nov 12 '13 at 20:44
  • 1
    How about logging to different, non-file-based log targets (like Event Log or ASP.NET Trace) from within NLog to see whether it is a file issue? – Uwe Keim Nov 12 '13 at 20:47
  • 1
    @UweKeim I had a Database target setup, but logging was as erratic as with the file target... I'll change the APP POOL to another one in which there is an application that is logging correctly... – Loudenvier Nov 12 '13 at 20:49
  • 1
    @UweKeim I'm beginning to doubt it is an NLog-related problem. The NSec logger is a custom HttpModule and I believe that it is simply NOT BEING RUN under IIS8, so the log actually never happened! Now why this HttpModule runs under IIS 6, 7, Express and Cassini but not IIS8... I think I'll have to close this question. – Loudenvier Nov 12 '13 at 21:36
  • 1
    Hopefully I could give you at least some help :-) – Uwe Keim Nov 12 '13 at 21:45

2 Answers2

3

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 :-(

Community
  • 1
  • 1
Loudenvier
  • 8,362
  • 6
  • 45
  • 66
0

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.

M Akin
  • 446
  • 6
  • 18