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>
It's maybe your service is running under an other user context and also because of Windows restrictions. I had the same issue and solved it logging into the following folder:
Maybe this will help you.
I found this post very helpful when I had the same problem:
http://nlog-forum.1685105.n2.nabble.com/Nlog-not-working-with-Windows-service-tp6711077p6825698.html
Basically, you'll want to include ${basedir} as part of your file location in your config. This will make NLog start at where your executable is running from.
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
I had a very closely related problem. My NLOG looked like this:
It was all permission related. Firstly, where I installed the service, I had to make sure the LOCAL SERVICE account had permission to read/write to the Logs folder.
Secondly, the internalLogFile although not written to, Nlog appears to try and access regardless - which is why I solved my issue by again ensuring LOCAL SERVICE has permission to read/write in **c:\temp**
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.
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.