Event Log Sample

Visual Basic offers two different logging mechanisms:

  • The EventLog component, used in this sample, provides access to the operating system event logs. You can add and delete logs, add and remove event log sources, write messages, and delete messages.

  • The My.Application.Log object also provides access to some of the EventLog features. In addition, My.Application.Log has features that enable you to write messages to text files. For more information, see Logging Sample and Logging Information from the Application.

To get samples and instructions for installing them, see the following:

  • Click Samples on the Visual Studio Help menu.

    For more information, see Locating Sample Files.

  • The most recent versions and the complete list of samples are available on the Visual Studio 2008 Samples Web site.

  • You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied to a folder under \Program Files\Visual Studio 9\Samples\. For Visual Studio Express Editions, all samples are located on the Internet.

To run this sample

  • Press F5.

    Not all operating systems support event logs. For more information, see EventLog.

Demonstrates

The main form, Form1, contains three Button controls to read, write, and create or delete event logs. Each opens a new form that collects information specific to the operation.

The form WriteForm collects the text, ID, and type for an event-log entry and writes the entry to the Application event log using the WriteEntry method.

The form ReadForm fills a ListBox control with the names of the event logs on the computer. The list is obtained by calling the GetEventLogs method, and then displaying the value of the LogDisplayName property for each log. The last ten entries of the selected log are retrieved with Entries property and then displayed in a RichTextBox control.

The form CreateDeleteForm calls the CreateEventSource and Delete methods. The SourceExists method is used to verify that the event log and source do not already exist before creating them. The Exists method is used to verify that the event log exists before deleting it.