I am trying to access the Event Code value (which is a unit) like this:
private static void EventLogMonitor(object sender, EventArrivedEventArgs e)
{
ManagementBaseObject targetInstance = (ManagementBaseObject)e.NewEvent.GetPropertyValue("TargetInstance");
EventId = (int)(targetInstance.GetPropertyValue("EventCode"));
...
But I get an invalid cast exception at (int)(targetInstance.GetPropertyValue("EventCode")). It seems this piece of code returns an object, I wonder how it is possible to access the uint Event Code value?
Assuming that you are dealing with
Win32_NTLogEventclasses, you need to cast toUInt16(orushort), as that is the type for “EventCode” according to msdn.