测试 EventID(已弃用)上的转换 (Uint16) 是否有效?

Testing to see if casting (Uint16) on EventID(deprecated) will be valid?

旧代码:if (eventArguments.Entry.EventID == 1074)

EventID 已弃用。根据 this 我可以将其更改为: 至

(UInt16)eventArguments.Entry.InstanceId == 1074

我正在尝试制作一个控制台应用程序来对此进行测试。谁能帮助我或给我一些指导?

edit- 这是我目前所拥有的,我认为足够了吗?

            Dictionary<string, EventLog> logList = new Dictionary<string, EventLog>();
        foreach (EventLog log in EventLog.GetEventLogs())
            logList.Add(log.LogDisplayName, log);

        foreach (EventLogEntry entry in logList["Application"].Entries)
        {
            long instanceID = entry.InstanceId;
            long eventID = entry.EventID;
            long calculatedEventID = entry.InstanceId & 0x3fffffff;
            //long calculatedEventID = (UInt16)entry.InstanceId;
            if (eventID != calculatedEventID)
                Console.WriteLine("{0}, {1}, {2}", eventID, instanceID, calculatedEventID);
            else
            {
                Console.WriteLine("calculatedEventID is {0} ", calculatedEventID);
            }
        }

为了保证您正在寻找的结果,您需要:

if (eventArguments.Entry.InstanceId & 0x3FFFFFFF == 1074)

您可以浏览 .NET Framework Source to find this out. Specifically, here's EventLogEntry 的源代码。这是 EventID 属性:

的代码
public int EventID {
    get {
        // Apparently the top 2 bits of this number are not
        // always 0. Strip them so the number looks nice to the user.
        // The problem is, if the user were to want to call FormatMessage(),
        // they'd need these two bits.
        return IntFrom(dataBuf, bufOffset + FieldOffsets.EVENTID) & 0x3FFFFFFF;
    }
}

这是 InstanceId 的代码:

public Int64 InstanceId {
    get {
        return (UInt32)IntFrom(dataBuf, bufOffset + FieldOffsets.EVENTID);
    }
}