EventLogEntryCollection.Item[Int32] Property

Definition

Gets an entry in the event log, based on an index that starts at 0 (zero).

C#
public virtual System.Diagnostics.EventLogEntry this[int index] { get; }

Parameters

index
Int32

The zero-based index that is associated with the event log entry.

Property Value

The event log entry at the location that is specified by the index parameter.

Examples

The following example demonstrates how to display information for the items in an EventLogEntryCollection object.

C#
// Create a new EventLog object.
EventLog myEventLog1 = new EventLog();
myEventLog1.Log = myLogName;
// Obtain the Log Entries of the Event Log
EventLogEntryCollection myEventLogEntryCollection = myEventLog1.Entries;
Console.WriteLine("The number of entries in 'MyNewLog' = " +
                        myEventLogEntryCollection.Count);
// Display the 'Message' property of EventLogEntry.
for (int i = 0; i < myEventLogEntryCollection.Count; i++)
{
    Console.WriteLine("The Message of the EventLog is :" +
                            myEventLogEntryCollection[i].Message);
}

Remarks

EventLogEntry objects are indexed by the event log system according to the chronological order in which they arrived in the event log. Use the Item[] property to select a specific event log entry whose index in the collection is known.

Iterating through the EventLogEntryCollection instance steps through each EventLogEntry object sequentially. The collection is dynamic and the number of entries may not be immutable when you enter the loop. Therefore, you should use a for each...next loop instead of a for(int i=0; i<count, i++) loop to step through entries that are associated with the EventLogEntryCollection instance to examine the entire set of entries.

Because new entries are appended to the existing list, stepping through the collection enables you to access the entries that were created after you originally created the EventLogEntryCollection.

Applies to

Tuote Versiot
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also