Export (0) Print
Expand All

EventLogEntry.InstanceId Property

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

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

[ComVisibleAttribute(false)]
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}...",
                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);

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft