NLog and Windows Event Log Bug? November 6, 2009
Posted by kevinbe71 in .NET, Development, Windows Development.add a comment
I’ve been experimenting with a logging library for .NET called NLog. My past experience has been with Log4Net but I’ve not been all that happy with Log4Net so I was looking for a better alternative. NLog seems to be that alternative… but I ran into one glitch when I switched from file and console logging to Windows event logging. I just wasn’t getting anything logged. The first problem was that I didn’t have an event log source defined, so I used WiX to create an event source (pretty easy to do since I’ve already used WiX for this purpose in the past). After that I was a bit puzzled- it still wasn’t working and the nlog internal logs weren’t showing anything either.
It was time to dig into the source code… and what I found out seems to be a bug in NLog. I had a target configured this way:
<target
name="eventlog"
xsi:type="EventLog"
layout="${longdate}|${level}|${message}"
log="Application"
source="My Source" />
I didn’t have machineName defined. After all, the documentation stated that machineName defaults to the local machine anyway, so why set it to anything? Well… because if you don’t then a boolean member var called _operational stays “false” and it needs to be set to “true” for the event log entry to get written! So, the workaround for this is actually pretty easy:
<target
name="eventlog"
xsi:type="EventLog"
layout="${longdate}|${level}|${message}"
log="Application"
source="My Source"
machineName="." />
Anyway, I thought someone else may find this useful if they’re also an NLog newbie…