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.

Logging 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.

To get samples and instructions for installing them

  • Do one or more of the following:

    • On the Help menu, click Samples.

      The Readme displays information about samples.

    • Visit the Visual Studio 2008 Samples Web site. The most recent versions of samples are available there.

    • Locate samples on the computer on which Visual Studio is installed. By default, samples and a Readme file are installed in drive:\Program Files\Microsoft Visual Studio 9.0\Samples\lcid. For Express editions of Visual Studio, all samples are located online.

For more information, see Visual Studio Samples.

Security noteSecurity Note:

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

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.

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

Community Additions

Show:
© 2014 Microsoft