Why won't my windows service write to my log f

2020-02-09 06:33发布

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>

12条回答
闹够了就滚
2楼-- · 2020-02-09 07:26

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.

查看更多
贪生不怕死
3楼-- · 2020-02-09 07:27

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 .

查看更多
虎瘦雄心在
4楼-- · 2020-02-09 07:30

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.

查看更多
迷人小祖宗
5楼-- · 2020-02-09 07:30

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.

查看更多
一纸荒年 Trace。
6楼-- · 2020-02-09 07:31

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.

查看更多
Anthone
7楼-- · 2020-02-09 07:34

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

查看更多
登录 后发表回答