Udostępnij za pośrednictwem


Walkthrough: Turning Off My.Application.Log Output (Visual Basic)

This walkthrough demonstrates how to turn off the default log filtering for the My.Application.Log object. You can change the logging behavior even after building the application, because the configuration information is stored in the application's configuration file.

Getting Started

The My.Application.Log object passes each message it gets to its log listeners. This sample application uses the My.Application.Log.WriteEntry methods to write a message to the listeners.

To build the sample application

  1. Open a new Visual Basic Windows Application project.

  2. Add a button named Button1 to Form1.

  3. In the Click event handler for Button1, add the following code:

    My.Application.Log.WriteEntry("Log entry")
    
  4. Run the application in the debugger.

  5. Press Button1.

    The application writes the following information to the application's debug output and log file.

    DefaultSource Information: 0 : Log entry

  6. Close the application.

For information on how to view the application's debug output window, see Output Window. For information on the location of the application's log file, see Walkthrough: Determining Where My.Application.Log Writes Information (Visual Basic).

Removing Listeners from My.Application.Log

By default, an application has two listeners that write to the application's debug output and the log file. This example demonstrates how to remove those listeners.

To remove log listeners from the Log object

  1. Right-click app.config in Solution Explorer and choose Open.

    -or-

    If there is no app.config file:

    1. On the Project menu, click Add New Item.

    2. In the Add New Item box, choose Application Configuration File.

    3. Click Add.

  2. Locate the <listeners> section in the <source> section with the name attribute "DefaultSource".

    These log configuration sections are located in the <system.diagnostics> node in the main <configuration> node of the configuration file. The XML for the DefaultSource is in the <sources> node.

  3. Remove the <add> element with the "FileLog" name attribute. It should look like this element:

    <add name="FileLog"/>

  4. Add this element to the <listeners> section.

    <!-- Remove the default debug listener. -->
    <remove name="Default"/>
    
  5. The content of the app.config file should be similar to the following XML:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.diagnostics>
        <sources>
          <!-- This section configures My.Application.Log -->
          <source name="DefaultSource" switchName="DefaultSwitch">
            <listeners>
              <!-- Remove the default debug listener. -->
              <remove name="Default"/>
            </listeners>
          </source>
        </sources>
        <switches>
          <add name="DefaultSwitch" value="Information" />
        </switches>
      </system.diagnostics>
    </configuration>
    
  6. Run the application in the debugger.

  7. Press Button1.

    The application writes no information to the application's log file or debug output.

For more information about changing log settings after deployment, see Working with Application Logs in Visual Basic.

See Also

Tasks

Walkthrough: Determining Where My.Application.Log Writes Information (Visual Basic)

Walkthrough: Changing Where My.Application.Log Writes Information (Visual Basic)

Walkthrough: Filtering My.Application.Log Output (Visual Basic)

Walkthrough: Creating Custom Log Listeners (Visual Basic)

How to: Write Log Messages (Visual Basic)

Concepts

Logging Information from the Application (Visual Basic)