问题
I've got the EventLog target set up like so:
<target xsi:type="EventLog"
name="EventLog"
layout="${longdate:universalTime=true}|${level:uppercase=true}|${logger}|${message}"
source="MyApp"
log="Application" />
Now, obviously not all my events will have the same ID, so I want to set event ID on a per message basis, rather than setting a static ID in the config. I believe this should work:
var logger = LogManager.GetCurrentClassLogger();
var logEvent = new LogEventInfo(LogLevel.Warn, logger.Name, "Test message");
logEvent.Properties.Add("EventID", 4444);
logger.Log(logEvent);
...but my events always have event ID set to 0. Anyone know how to get this working?
回答1:
I figured it out - you have to use a layout in the eventId
property of the target:
<target xsi:type="EventLog"
name="EventLog"
layout="${longdate:universalTime=true}|${level:uppercase=true}|${logger}|${message}"
source="MyApp"
>> eventId="${event-properties:EventID}" <<
log="Application" />
I've also created the Timber logging facade called for both NLog and log4net, which makes logging messages with different event IDs very simple.
回答2:
On the github hub repo there's an example config for EventLog targets that includes an eventId. The eventId will use a Layout that renders an event ID.
https://github.com/NLog/NLog/wiki/Eventlog-target
来源:https://stackoverflow.com/questions/17320933/set-event-id-per-log-when-writing-to-windows-event-log