Nlog does not write log file to Windows service
I have a windows service written in .net 4.0 and I installed it under Local System credentials. 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 the exe file is located
<?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 generated at all. If I change the code log.Debug
to Debug.WriteLine
and use my Visual Studio to attach to Windows processing for debugging, I see that there is my debug message in the output window, which means my Windows service code is correct.
Is there a problem with my nlog code?
- Update 1 -
I have 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'm not sure what is wrong with my first config.
source to share
UPDATE
<logger name="*" minlevel="Info" maxLevel="Deubg" writeTo="logfile" />
^^^^^
First of all: Debug
written incorrectly.
But the main problem is that the debug level is below the information level .
The following are the acceptable log levels (in descending order):
- from
- fatal
- mistake
- warn
- Information
- debug
- trace (the most detailed information.)
source to share