Mike Roosa
Mike Roosa

Reputation: 4802

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>

Upvotes: 32

Views: 57631

Answers (12)

Simon Miller
Simon Miller

Reputation: 970

I had a very closely related problem. My NLOG looked like this:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
  autoReload="true"
  throwExceptions="false"
  internalLogLevel="Off"
  internalLogFile="c:\temp\nlog-internal.log">

<targets>
<!-- Write events to a file with the date in the filename -->
<target xsi:type="File"
  name="File"
  fileName="${basedir}/logs/${shortdate}.log"
  layout="${longdate} ${uppercase:${level}} ${message}" />
</targets>

<rules>
<!-- Exception levels: Fatal, Error, Warn, Info, Debug, Trace -->
<logger name="*"
  minlevel="Debug"
  writeTo="File" />
</rules>

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

Upvotes: 0

KABIA Edouard
KABIA Edouard

Reputation: 176

hi this is what i did and it's work nicely u have to create class library and in this class add the following methode ^^

  public static void WriteErrorLog(Exception ex)
    {
        StreamWriter sw = null;
        try
        {
            sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\LogFile.txt", true);
            sw.WriteLine(DateTime.Now.ToString() + ":" + ex.Source.ToString().Trim() + ":" + ex.Message.ToString().Trim());
            sw.Flush();
            sw.Close();
        }
        catch
        {

        }
    }
    public static void WriteErrorLog(String Message)
    {
        StreamWriter sw = null;
        try
        {
            sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\LogFile.txt", true);
            sw.WriteLine(DateTime.Now.ToString() + ":"+Message);
            sw.Flush();
            sw.Close();
        }
        catch
        {

        }
    }

and in your service u have to make in OnStart method :

Library.WriteErrorLog(" Service Started ");
//and in your OnStop method
     Library.WriteErrorLog(" Service Stoped ");

hope this will be helpfull .

Upvotes: -4

Stefan Cvetanovski
Stefan Cvetanovski

Reputation: 298

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.

Upvotes: 15

jwatts1980
jwatts1980

Reputation: 7346

After creating an installation project for my service, and installing it multiple times, I finally realized that I had not included the NLog.config file as one of the files to install. Now that it is included alongside the executable, it's working perfectly.

For what it's worth, the NLog.config file can be manually added after the fact, but the service may need to be stopped and restarted.

Upvotes: 0

bressain
bressain

Reputation: 898

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.

Upvotes: 2

Aykut &#199;evik
Aykut &#199;evik

Reputation: 2088

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:

Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)

Maybe this will help you.

Upvotes: 0

27k1
27k1

Reputation: 2369

I have just had the same problem with Enterprise framework logging.

To conclude this question of which the Answers together tell the correct story.

In your example when using the Visual Studio IDE the log file is being written using the application's user permissions and the log file is being written.

The Windows Service does not have these same permissions so the log file will not get written. Windows Service does have permission (I have tested this) to write to the

AppDomain.CurrentDomain.BaseDirectory

using System.IO namespace.

So direct the log file to this base directory and you will be safe.

Upvotes: 2

Tim Clem
Tim Clem

Reputation: 1270

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);

Upvotes: 33

genki
genki

Reputation: 301

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

Upvotes: 2

Richard
Richard

Reputation: 108975

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.

Upvotes: 3

Eoin Campbell
Eoin Campbell

Reputation: 44268

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.

Upvotes: 1

Michael Meadows
Michael Meadows

Reputation: 28416

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.

Upvotes: 14

Related Questions