Set Event ID per log when writing to Windows Event Log

為{幸葍}努か 提交于 2019-12-01 07:04:48

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!