Uređivanje

Dijeli putem


EventLogEntry.InstanceId Property

Definition

Gets the resource identifier that designates the message text of the event entry.

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

Property Value

A resource identifier that corresponds to a string definition in the message resource file of the event source.

Attributes

Examples

The following code example searches an event log for entries with a particular resource identifier. The code example displays the event message for each matching entry, and counts the total number of matching entries in the log. The message text for each entry may or may not be the same; each event message depends on the event source message file, insertion strings, and parameters used when it was written.


// 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)

Remarks

The InstanceId property uniquely identifies an event entry for a configured event source. The InstanceId for an event log entry represents the full 32-bit resource identifier for the event in the message resource file for the event source. The EventID property equals the InstanceId with the top two bits masked off. Two event log entries from the same source can have matching EventID values, but have different InstanceId values due to differences in the top two bits of the resource identifier.

If the application wrote the event entry using one of the WriteEntry methods, the InstanceId property matches the optional eventId parameter. If the application wrote the event using WriteEvent, the InstanceId property matches the resource identifier specified in the InstanceId of the instance parameter. If the application wrote the event using the Windows API ReportEvent, the InstanceId property matches the resource identifier specified in the dwEventID parameter.

For details about defining event messages and building event log resource files, see the Message Compiler article in the Platform SDK documentation. For details about event log identifiers, see the Event Identifiers article in the Platform SDK documentation.

Applies to

See also