共用方式為


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 資源識別碼。

如需定義事件訊息和建置事件記錄檔資源文件的詳細資訊,請參閱 Platform SDK 檔中的 訊息編譯 程式一文。 如需事件記錄檔標識碼的詳細資訊,請參閱平臺 SDK 檔中的事件 識別碼 一文。

適用於

另請參閱