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.
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):
Link to the nlog documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With