Logging Sample

Visual Basic offers two different logging mechanisms. My.Application.Log provides access to the operating system event logs and also allows you to write messages to text files. The EventLog component provides access to the operating system event logs. The EventLog component allows you to add and delete logs, add and remove event log sources, write messages, and delete messages. For sample code, see Event Log Sample.

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

  1. Press F5. The main form lists the listeners that are configured for the sample.

  2. Write a message in the TextBox control on the form.

  3. Click the Write Log Message button.

    The message is written to each of the listeners.

    1. The XML file written by the XmlWriterTraceListener resides at c:\logsamples\SampleLog.xml.

    2. The plain-text file written by the FileLogTraceListener resides in your "Application Data" directory. If you are running the program in Visual Studio, the path will look something like "c:\Documents And Settings\[user]\Application Data\Microsoft Corporation\Microsoft Visual Studio\[8.0*]\LogSample.log.

Demonstrates

This sample contains:

  • Form1   Provides a simple form that shows all listeners currently enabled for My.Application.Log and has controls that allow text to be written to My.Application.Log.

  • app.config   Defines the configuration for My.Application.Log — where messages should be written, which messages should be filtered, and so on.

This sample demonstrates how My.Application.Log can be used to write a plain text file, the event log, and an XML file. You can easily implement a custom TraceListener that writes to other locations as well, such as a database or an e-mail message.

Each message is assigned the "Severity" selected on the form. When the listener receives the message, it may discard the message based on its filter level. For example, the EventLogTraceListener component is configured to write error messages only, so any messages you write with a severity "Information" or "Warning" will be written to the XML and plain text file and not to the EventLog.

To change the behavior of the sample, edit the app.config file of the project. Comments in the file serve as a guide to changes.

You can use My.Application.Log and the application model to log startup, shutdown, and unhandled exceptions. To see the code for this sample, double-click My Project in Solution Explorer. On the Application pane, click the View Code button.