asp.net-web-api2nlog

Can't get NLog to throw an exception or write a log (works on my machine)


I have a .Net webapi running on my workstation (Win10, IIS 10) and everything works like a charm. However when I push it to our production box (Win7, IIS 6) NLog will neither write a log nor throw an exception. My NLog.config is here:

 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      throwExceptions="true"
   >

  <targets async="true">
    <target
      name="logfile"
      xsi:type="File"
      layout="${date}|${level:uppercase=true}${message}"
      fileName="C:\inetpub\wwwroot\myapp\App_Data\webapi.log"
    />
  </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="logfile" />
  </rules>
</nlog>

No matter what I set the fileName property to, it will not write a log on the Win7 box and does not throw an exception. I would see the exception in the Event Viewer under Windows Logs -> System, correct?

Note that I do not have administrative access to the target Win7 box but have enough elevated privileges to write files to wwwroot.

What am I missing? An exception or some type of error would be nice.

Edit: in fact it does NOT work at all on IIS10/Win10 though it does work fine when running in Visual Studio (IIS Express?). Both Visual Studio and IIS Manager are being run "as administrator". No logfile output, no exceptions, nothing in the Event Viewer. ???


Solution

  • There is a NLog Troubleshooting guide on https://github.com/NLog/NLog/wiki/Logging-Troubleshooting

    When you get no log output from NLog, this can be because of the following reasons:

    • NLog cannot find the configuration file. This can happen when the NLog.config file is configured with Build Action = None or Copy to Output Directory = Do not copy in Visual Studio. Set Build Action = Content and "Copy to Output Directory = Copy if newer to fix this)
    • Is your configuration file valid XML?
    • Logging rules are incorrect or no rules are defined
    • Application tracing code is incorrect.
    • There is a runtime problem with the target (such as missing permissions)
    • Logs are written to a different location.

    Also you could enable the internal log: https://github.com/NLog/NLog/wiki/Internal-logging

    Update: if your internal log isn't working, then it would be difficult to diagnose the issue.

    You could enable throwing exceptions from code:

    LogManager.ThrowExceptions = true;
    LogManager.ThrowConfigExceptions = true;