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

℡╲_俬逩灬. 提交于 2019-11-29 01:09:20

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.

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

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.

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.

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.

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.

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

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.

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.

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.

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

KABIA Edouard

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 .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!