Nlog does not write to log file in windows service
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
hardywang
Updated on June 04, 2022Comments
-
hardywang almost 2 years
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 toDebug.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.
-
Matthias R. over 9 yearsfor deeper understanding when to use which log level see nlog documentation too
-
Dave Rael over 9 years"Frist of all: debug is spelled wrong." - if transposing the characters in first when pointing out that characters in debug are transposed was deliberate, brilliant and clever.