Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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> 
like image 688
Mike Roosa Avatar asked May 13 '09 15:05

Mike Roosa


People also ask

Can Windows Service write to log file?

By default, logging is turned on for any service you create with the Windows Service project template. You can use a static form of the EventLog class to write service information to a log without having to create an instance of an EventLog component or manually register a source.

Where are logs for Windows services?

Click Start > Control Panel > System and Security > Administrative Tools. Double-click Event Viewer. Select the type of logs that you wish to review (ex: Windows Logs)

How do I open a logfile file?

You can read a LOG file with any text editor, like Windows Notepad. You might be able to open one in your web browser, too. Just drag it directly into the browser window, or use the Ctrl+O keyboard shortcut to open a dialog box to browse for the file.


2 Answers

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); 
like image 53
Tim Clem Avatar answered Oct 11 '22 05:10

Tim Clem


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.

like image 42
Michael Meadows Avatar answered Oct 11 '22 06:10

Michael Meadows