This documentation is archived and is not being maintained.

Logging Sample 

Download sample

This sample demonstrates how to use and configure the My.Application.Log object.

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.

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

  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.


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.

See Also