views:

2313

answers:

5

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>
+5  A: 

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.

Michael Meadows
or just give local service permission to write to that directory in the installer.
Darren Kopp
how do I give permission in the installer? also the nlog.config is in the project bin directory but not getting moved to the setup file. how can I make that happen
Mike Roosa
+2  A: 

Have you tried install/run your service as a different named user.

If that works, then you can be pretty sure you've a permissions issue where your Local system account doesn't have permission to write to the directory/file.

Eoin Campbell
A: 

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.

Richard
thanks for the tip richard, this made me realize the nlog.config file wasn't there. loaded it and still not working but i'm sure that's part of the problem
Mike Roosa
+1  A: 

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...

genki
+3  A: 

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);
Tim Clem
doesn't look like it's in system32
Mike Roosa