Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nlog does not write to log file in windows service

I have a windows service written in .net 4.0 and I installed it under credential of Local System. In the code I used nlog,

private static Logger logger = LogManager.GetCurrentClassLogger();
logger.Debug("some information");

I also have nlog.config copied to the same directory where exe file resides

<?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="logfile" type="File"
            fileName="c:\log\TestService\My.log"
            layout="${longdate}::${logger}::${message}"
            keepFileOpen="false" />
  </targets>

  <rules>
    <logger name="*" minlevel="Info" maxLevel="Deubg" writeTo="logfile" />
  </rules>
</nlog>

But if I start the service, I don't see the log file has been created at all. If I swap the log.Debug code to Debug.WriteLine and use my Visual Studio to attach to the windows service process to debug, I can see the output window has my debugging message, which means my windows service code is correct.

Is there any problem with my nlog code?

-- Update 1 --

I updated nlog.config to

<?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="logfile" type="File"
            fileName="c:\log\TestService\My.log"
            layout="${longdate}::${logger}::${message}"
            keepFileOpen="false" />
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="logfile" />
  </rules>
</nlog>

and changed my code to

logger.Trace("some information");

then it works. I am not sure what is wrong with my first set of configuration.

like image 556
hardywang Avatar asked Aug 07 '14 13:08

hardywang


1 Answers

UPDATE

<logger name="*" minlevel="Info" maxLevel="Deubg" writeTo="logfile" />
                                           ^^^^^

First of all: Debug is spelled wrong.

But the main problem is that the Debug level is under the info level.

The following are the allowed log levels (in descending order):

  • off
  • fatal
  • error
  • warn
  • info
  • debug
  • trace (Most detailed information.)

Link to the nlog documentation

like image 134
SteMa Avatar answered Oct 19 '22 23:10

SteMa