EventLogEntry.InstanceId Property


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

Namespace:   System.Diagnostics
Assembly:  System (in System.dll)

public long InstanceId { get; }

Property Value

Type: System.Int64

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

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 Win32 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" topic in the Platform SDK documentation at http://msdn.microsoft.com. For details about event log identifiers, see the "Event Identifiers" topic in the Platform SDK.

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 = new EventLog(myLogName, ".");
int count = 0;

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

// 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("Entry ID    = {0}", 
        Console.WriteLine("Reported at {0}", 
        Console.WriteLine("Message text:");
        Console.WriteLine("\t{0}", entry.Message);
Console.WriteLine("Found {0} events with ID {1} in event log {2}.",
    count.ToString(), ServerConnectionDownMsgId.ToString(), myLogName);

.NET Framework
Available since 2.0
Return to top