Event Log 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:
Thecomponent, 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, seeand .
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
Click Download Sample.
The File Download message box appears.
Click Open, and on the left column of the zip folder window, click Extract all files.
The Extraction Wizard opens.
Click Next. You can change the directory that the files will be extracted to, and then click Next again.
Make sure that the Show extracted files check box is selected, and click Finish.
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
Not all operating systems support event logs. For more information, see EventLog.
The main form, Form1, contains threecontrols 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 themethod.
The form ReadForm fills acontrol with the names of the event logs on the computer. The list is obtained by calling the method, and then displaying the value of the property for each log. The last ten entries of the selected log are retrieved using the property and then displayed in a control.
The form CreateDeleteForm calls theand methods. The method is used to verify that the event log and source do not already exist before creating them. The method is used to verify that the event log exists before deleting it.