NLog does not create a log file

Solution 1:

I had this problem turned out that my log file was not being copied to my build directory. The NLog github page had the answer. (I've reformatted the paragraph a little for better readability.) https://github.com/NLog/NLog/wiki/Logging-troubleshooting

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)

Solution 2:

The log file was being created - but not in the application directory.

Using ${basedir} layout renderer as part of the file name proved to be a solution.

Solution 3:

In case the response marked as answer is not all that clear you can check the example

<targets>
  <target xsi:type="Console" name="console" 
    layout="${longdate}|${level}|${message}" />
  <target xsi:type="File" name="ErrorLog" fileName="${basedir}/error.txt"
          layout="${longdate}
          Trace: ${stacktrace} 
          ${message}" />
  <target xsi:type="File" name="AccessLog" fileName="${basedir}/access.txt"
          layout="${shortdate} | ${message}" />
</targets>

Taken from here using AppData location in NLog

Solution 4:

from nlog troubleshooting guide

Please check Nlog.config file properties: Copy to output directory should be Copy always

Please view image link https://i.stack.imgur.com/AlUG5.png

Solution 5:

My issue was permission related, the log file needs to allow the process to write to it, without write permissions you'll get no file.

Here's how to fix it for websites in IIS:

  1. Right click on your folder in windows explorer and select properties
  2. Choose the security tab
  3. Click edit
  4. Click add
  5. In the textbox type 'IIS AppPool\YourAppPoolName' replace YourAppPoolName with the actual name of the application pool your site runs under
  6. Click Check names
  7. Click OK

Security footnote: From a security aspect the best practice is to use ApplicationPoolIdentity as it is a dynamically created, unprivileged account. more reading here: https://docs.microsoft.com/en-us/iis/manage/configuring-security/application-pool-identities