Search code examples
.net.net-4.5nlogwindows-server

NLog file target writes only one record


Whenever my application runs on the Test, Preprod or Prod server (Windows 2012 R2) on the infrastructure of our hosting provider, all file targets write exactly one log entry, and then stop working without any indication of the reason. If I delete the log file (or if it gets archived, for example on the next day), a new log file is written, but again with only one log record.

The same problem is described on the NLog forums here, but without a workaround or explanation: http://nlog-forum.1685105.n2.nabble.com/Only-one-entry-written-to-log-file-td7399596.html

I tried the following things:

  • Enabled the internal logging of NLog. Same problem here, the log file with the internal logging contained exactly one log item. Internal logging can only be set up with a file target, so there was no way to listen to the internal logging on another target (like DebugView).
  • Listened with Sysinternals DebugView for internal error messages of NLog, but nothing.
  • Checked the log files read-only attribute, but it was set to false.
  • Switched all targets to async. No effect.

Any other ideas? I cannot run Visual Studio on those servers, so there's no way I can debug it with the NLog source.

Would be a pity to switch to log4net because of a silly bug.

EDIT: I have to add that the other targets (like DebugView or ADO.NET) work fine, it's really a problem of the file target. And because the internal logging is restricted to a file target, I cannot see how to find the cause of the problem


Solution

  • Seems to be a permissions problem. Once I granted the users group IIS_IUSRS write rights to the logs folder, everything was working as expected.

    I'm just wondering, why could it write the log file with the one entry?

    I never thought about a permissions problem because I saw that IIS could write the log file.

    EDIT: As suggested in the comments, I switched on the ${identity} log, and received the following values:

    first 3 lines:

    notauth::
    

    all following lines:

    auth:ApplicationCookie:{LOGIN_OF_CURRENT_USER}
    

    `{LOGIN_OF_CURRENT_USER} is the e-mail address of the user which is currently signed in.

    EDIT 2: Here's the icacls result:

    D:\IIS_Server\wwwroot\tgw\logs NT AUTHORITY\IUSR:(OI)(CI)(F)
                                   BUILTIN\IIS_IUSRS:(OI)(CI)(F)
                                   NT AUTHORITY\IUSR:(I)(OI)(CI)(RX)
                                   BUILTIN\IIS_IUSRS:(I)(OI)(CI)(RX)
                                   NT SERVICE\WMSVC:(I)(OI)(CI)(F)
                                   NT AUTHORITY\SYSTEM:(I)(OI)(CI)(F)
                                   BUILTIN\Administrators:(I)(OI)(CI)(F)
                                   BUILTIN\Users:(I)(CI)(AD)
                                   BUILTIN\Users:(I)(CI)(WD)
                                   BUILTIN\Users:(I)(OI)(CI)(RX)
    

    Please note that this is after I explicitly granted IIS_IUSRS write rights to the folder.