Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Event Log Sample 

Download sample

This sample demonstrates how to create and delete event logs, and how to read and write to system event logs and custom event logs.

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 allow you to write messages to text files. For more information, see Logging Sample and Logging Information from the Application.

Security noteSecurity Note

This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To open the sample file in Solution Explorer

  1. Click Download Sample.

    The File Download message box appears.

  2. Click Open, and on the left column of the zip folder window, click Extract all files.

    The Extraction Wizard opens.

  3. Click Next. You can change the directory that the files will be extracted to, and then click Next again.

  4. Make sure that the Show extracted files check box is selected, and click Finish.

  5. Double-click the sample's .sln file.

    The sample solution is displayed in Solution Explorer. You might get a security warning that says the solution location is not trusted. Click OK to continue.

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 using the 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.

See Also

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2015 Microsoft