30

I am using a website project, I added Nlog from Nuget, and used the following NLog configuration :

<?xml version="1.0" encoding="utf-8" ?>
<nlog autoReload="true" 
      throwExceptions="true" 
      xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
        <target name="logfile" xsi:type="File" fileName="log.txt" />
    </targets>

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

I ran ProcessMonitor and it shows that Nlog is trying to create log.txt in the IISExpress folder instead of my website project folder.

C:\Program Files (x86)\IIS Express\log.txt

How can I make NLog put my log file, log.txt, in my website project's folder instead of the IISExpress folder?

FarFigNewton
  • 7,108
  • 13
  • 50
  • 77

1 Answers1

56

We figured it out.

Add ${basedir} to the filename

filename="${basedir}/log.txt"
FarFigNewton
  • 7,108
  • 13
  • 50
  • 77
  • 1
    Incidentally, this shouldn't be needed any more; as of version 4.3 relative paths should be computed as relative to `${basedir}`. Tempus fugit. – Jon Hanna Oct 18 '17 at 11:07
  • and in a desktop application, if you omit some "..\" at the start (i.e. "Logs\mylog.log" rather than "..\Logs\mylog.log"), it'll end up in C:\Users\username\AppData\local\ *sigh* – smirkingman Jan 15 '21 at 15:26