EventLogEntry.InstanceId 属性

定义

获取指定事件项的消息文本的资源标识符。

public:
 property long InstanceId { long get(); };
public long InstanceId { get; }
[System.Runtime.InteropServices.ComVisible(false)]
public long InstanceId { get; }
member this.InstanceId : int64
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.InstanceId : int64
Public ReadOnly Property InstanceId As Long

属性值

资源标识符,与事件源的消息资源文件中的字符串定义对应。

属性

示例

下面的代码示例在事件日志中搜索具有特定资源标识符的条目。 该代码示例显示每个匹配项的事件消息,并计算日志中匹配条目的总数。 每个条目的消息文本可能相同,也可能不同;每个事件消息取决于事件源消息文件、插入字符串和写入时使用的参数。


// Get the event log corresponding to the existing source.
String^ myLogName = EventLog::LogNameFromSourceName( sourceName, "." );

// Find each instance of a specific event log entry in a
// particular event log.
EventLog^ myEventLog = gcnew EventLog( myLogName,"." );
int count = 0;
Console::WriteLine( "Searching event log entries for the event ID {0}...", ServerConnectionDownMsgId );

// Search for the resource ID, display the event text,
// and display the number of matching entries.
System::Collections::IEnumerator^ myEnum = myEventLog->Entries->GetEnumerator();
while ( myEnum->MoveNext() )
{
   EventLogEntry^ entry = safe_cast<EventLogEntry^>(myEnum->Current);
   if ( entry->InstanceId == ServerConnectionDownMsgId )
   {
      count++;
      Console::WriteLine();
      Console::WriteLine( "Entry ID    = {0}", entry->InstanceId );
      Console::WriteLine( "Reported at {0}", entry->TimeWritten );
      Console::WriteLine( "Message text:" );
      Console::WriteLine( "\t{0}", entry->Message );
   }
}

Console::WriteLine();
Console::WriteLine( "Found {0} events with ID {1} in event log {2}.", count, ServerConnectionDownMsgId, myLogName );
// Get the event log corresponding to the existing source.
string myLogName = EventLog.LogNameFromSourceName(sourceName,".");

// Find each instance of a specific event log entry in a
// particular event log.

EventLog myEventLog = new EventLog(myLogName, ".");
int count = 0;

Console.WriteLine("Searching event log entries for the event ID {0}...",
    ServerConnectionDownMsgId.ToString());

// Search for the resource ID, display the event text,
// and display the number of matching entries.

foreach(EventLogEntry entry in myEventLog.Entries)
{
    if (entry.InstanceId == ServerConnectionDownMsgId)
    {
        count ++;
        Console.WriteLine();
        Console.WriteLine("Entry ID    = {0}",
            entry.InstanceId.ToString());
        Console.WriteLine("Reported at {0}",
            entry.TimeWritten.ToString());
        Console.WriteLine("Message text:");
        Console.WriteLine("\t{0}", entry.Message);
    }
}
Console.WriteLine();
Console.WriteLine("Found {0} events with ID {1} in event log {2}.",
    count.ToString(), ServerConnectionDownMsgId.ToString(), myLogName);
    ' Get the event log corresponding to the existing source.
    Dim myLogName As String = EventLog.LogNameFromSourceName(sourceName,".")

    ' Find each instance of a specific event log entry in a
    ' particular event log.

    Dim myEventLog As EventLog = new EventLog(myLogName, ".", sourceName)
    Dim count As Integer = 0

    Console.WriteLine("Searching event log entries for the event ID {0}...", _
       ServerConnectionDownMsgId.ToString())
    
    ' Search for the resource ID, display the event text,
    ' and display the number of matching entries.

    Dim entry As EventLogEntry
    For Each entry In  myEventLog.Entries
        If entry.InstanceId = ServerConnectionDownMsgId
            count = count + 1
            Console.WriteLine()
            Console.WriteLine("Entry ID    = {0}", _
                entry.InstanceId.ToString())
            Console.WriteLine("Reported at {0}", _
                entry.TimeWritten.ToString())
            Console.WriteLine("Message text:")
            Console.WriteLine(ControlChars.Tab + entry.Message)
        End If
    Next entry

    Console.WriteLine()
    Console.WriteLine("Found {0} events with ID {1} in event log {2}", _
        count.ToString(), ServerConnectionDownMsgId.ToString(), myLogName)

注解

属性 InstanceId 唯一标识已配置事件源的事件条目。 事件日志条目的 表示 InstanceId 事件源的消息资源文件中事件的完整 32 位资源标识符。 属性 EventID 等于 , InstanceId 前两个位被屏蔽。 来自同一源的两个事件日志条目可以具有匹配 EventID 的值,但由于资源标识符的前两位存在差异,因此具有不同的 InstanceId 值。

如果应用程序使用方法之 WriteEntry 一编写事件条目,则 InstanceId 属性将匹配可选 eventId 参数。 如果应用程序使用 WriteEvent编写事件,则 InstanceId 属性与 参数 中指定的InstanceIdinstance资源标识符匹配。 如果应用程序使用 Windows API ReportEvent编写事件,则 InstanceId 属性与 参数中指定的 dwEventID 资源标识符匹配。

有关定义事件消息和生成事件日志资源文件的详细信息,请参阅平台 SDK 文档中 的消息编译器 一文。 有关事件日志标识符的详细信息,请参阅平台 SDK 文档中 的事件标识符 一文。

适用于

另请参阅