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
Open a new Visual Basic Windows Application project.
Add a button named Button1 to Form1.
In the Click event handler for Button1, add the following code:
My.Application.Log.WriteEntry("Log entry")
Run the application in the debugger.
Press Button1.
The application writes the following information to the application's debug output and log file.
DefaultSource Information: 0 : Log entry
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
Right-click app.config in Solution Explorer and choose Open.
-or-
If there is no app.config file:
On the Project menu, click Add New Item.
In the Add New Item box, choose Application Configuration File.
Click Add.
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.
Remove the <add> element with the "FileLog" name attribute. It should look like this element:
<add name="FileLog"/>
Add this element to the <listeners> section.
<!-- Remove the default debug listener. --> <remove name="Default"/>
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>
Run the application in the debugger.
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)