Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual Basic
Samples
 Event Log Sample
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual Basic: Samples 
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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker