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条回答
▲ chillily
2楼-- · 2020-02-09 07:14

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.

查看更多
你好瞎i
3楼-- · 2020-02-09 07:16

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.

查看更多
放我归山
4楼-- · 2020-02-09 07:19

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);
查看更多
我想做一个坏孩纸
5楼-- · 2020-02-09 07:24

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

查看更多
成全新的幸福
6楼-- · 2020-02-09 07:25

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.

查看更多
乱世女痞
7楼-- · 2020-02-09 07:26

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.

查看更多
登录 后发表回答