Set event id for each log when writing to Windows event log

I have an EventLog target set up:

<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 identifier, so I want to set the event identifier for each message, and not set a static identifier 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 an event identifier of 0. Does anyone know how to do this?

+2
source share
2 answers

I realized this - you need to use the layout in the eventId property for the purpose:

 <target xsi:type="EventLog" name="EventLog" layout="${longdate:universalTime=true}|${level:uppercase=true}|${logger}|${message}" source="MyApp" >> eventId="${event-properties:EventID}" << log="Application" /> 

I also created a Timber logging facade, called both NLog and log4net, which greatly simplifies logging messages with different event IDs.

+9
source

The gubub hub repo has an example configuration for EventLog objects that include eventId. EventId will use a layout that displays the identifier of the event.

https://github.com/NLog/NLog/wiki/Eventlog-target

0
source

Source: https://habr.com/ru/post/951498/


All Articles