Search code examples
c#loggingwindows-servicesservicenlog

Why won't my windows service write to my log file?


I have a windows service and use nlog for logging. Everything works fine when I run from the visual studio ide. The log file updates with no issues. When I install the service, the service runs fine but the log file never updates. I am running under LOCAL SERVICE if that helps. Yes, I have created the logs directory under my application folder.

 <?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 name="file" xsi:type="File" fileName="${basedir}/logs/${shortdate}_info.txt"
            layout="${date} ${logger} ${message}" />
  </targets>

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

Solution

  • Your local service account doesn't have access to write to the file location specified. You set it to use a system account in the "Log On" tab of the service properties dialog, or you can set up the user account as part of the setup process.