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

51,335

Solution 1

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.

Solution 2

I've had this issue too. As mentioned by genki you are probably logging into the \Windows\System32 directory. Maybe check for the log file you are expecting there first. When writing services I've often put a line like this in the beginning to get the current directory to behave like a normal application

Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);

Solution 3

If you are using x64 version of Windows than the log file is saved in C:\Windows\SysWOW64 folder

This is the default case if you build your project using the AnyCPU configuration and deploy to a 64 bit operating system.

Solution 4

Just out of curiousity, have you checked whether anything is being written in the system32 directory of your windows installation? Iirc, that's the default application runtime base directory for services...

Solution 5

You can use Process Monitor to look at the file operations being performed, and why they are failing.

I would suspect (along with other answerers) that this is a permission problem, with the service's account not having sufficient access to the file.

Share:
51,335
Mike Roosa
Author by

Mike Roosa

Updated on July 09, 2022

Comments

  • Mike Roosa
    Mike Roosa almost 2 years

    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>