How to: Read Event Log Entries

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

You use the Entries collection to look at the entries in a particular event log. You can use standard collection properties such as Count and Item to work with the elements the collection contains. You might read event log entries to find out more information about a problem that occurred in your system, to identify usage patterns, or to identify problems (such as a failing hard disk drive) before they cause damage.

Note Note

The Entries collection is read-only; to write a message to a log you must use the WriteEntry method. For information, see How to: Write Entries to Event Logs.

If you ask for the count of entries in a new custom log that has not yet been written to, the system will return the count of the entries in the Application log on that server. Make sure that logs you are counting have been created and written to in order to avoid this problem.

Note Note

There are security restrictions that affect your ability to use event logs. For more information, see Security Ramifications of Event Logs.

Security note Security Note

Treat the data from an event log as you would any other input coming from outside your system. Your application may need to validate the data in the event log before using it as input. Another process, possibly a malicious one, may have accessed the event log and added entries.

To read event log entries

  1. Create an instance of the EventLog component. For more information, see How to: Create EventLog Component Instances.

  2. Set the Log and MachineName properties for the component. For more information, see How to: Configure EventLog Component Instances.

  3. Use the Entries collection to review the entries in the log. The following example shows how to retrieve all of the entries from a log.

    		foreach (System.Diagnostics.EventLogEntry entry in EventLog1.Entries)