Walkthrough: Changing Where My.Application.Log Writes Information (Visual Basic)
You can use the My.Application.Log
and My.Log
objects to log information about events that occur in your application. This walkthrough shows how to override the default settings and cause the Log
object to write to other log listeners.
Prerequisites
The Log
object can write information to several log listeners. You need to determine the current configuration of the log listeners before changing the configurations. For more information, see Walkthrough: Determining Where My.Application.Log Writes Information.
You may want to review How to: Write Event Information to a Text File or How to: Write to an Application Event Log.
To add listeners
Right-click app.config in Solution Explorer and choose Open.
- or -
If there is no app.config file:
On the Project menu, choose Add New Item.
From the Add New Item dialog box, select Application Configuration File.
Click Add.
Locate the
<listeners>
section, under the<source>
section with thename
attribute "DefaultSource", in the<sources>
section. The<sources>
section is in the<system.diagnostics>
section, in the top-level<configuration>
section.Add these elements to that
<listeners>
section.<!-- Uncomment to connect the application file log. --> <!-- <add name="FileLog" /> --> <!-- Uncomment to connect the event log. --> <!-- <add name="EventLog" /> --> <!-- Uncomment to connect the event log. --> <!-- <add name="Delimited" /> --> <!-- Uncomment to connect the XML log. --> <!-- <add name="XmlWriter" /> --> <!-- Uncomment to connect the console log. --> <!-- <add name="Console" /> -->
Uncomment the log listeners that you want to receive
Log
messages.Locate the
<sharedListeners>
section, in the<system.diagnostics>
section, in the top-level<configuration>
section.Add these elements to that
<sharedListeners>
section.<add name="FileLog" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" initializeData="FileLogWriter" /> <add name="EventLog" type="System.Diagnostics.EventLogTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" initializeData="sample application"/> <add name="Delimited" type="System.Diagnostics.DelimitedListTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" initializeData="c:\temp\sampleDelimitedFile.txt" traceOutputOptions="DateTime" /> <add name="XmlWriter" type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" initializeData="c:\temp\sampleLogFile.xml" /> <add name="Console" type="System.Diagnostics.ConsoleTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" initializeData="true" />
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> <add name="FileLog"/> <!-- Uncomment to connect the application file log. --> <!-- <add name="FileLog" /> --> <!-- Uncomment to connect the event log. --> <!-- <add name="EventLog" /> --> <!-- Uncomment to connect the event log. --> <!-- <add name="Delimited" /> --> <!-- Uncomment to connect the XML log. --> <!-- <add name="XmlWriter" /> --> <!-- Uncomment to connect the console log. --> <!-- <add name="Console" /> --> </listeners> </source> </sources> <switches> <add name="DefaultSwitch" value="Information" /> </switches> <sharedListeners> <add name="FileLog" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" initializeData="FileLogWriter" /> <add name="EventLog" type="System.Diagnostics.EventLogTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" initializeData="sample application"/> <add name="Delimited" type="System.Diagnostics.DelimitedListTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" initializeData="c:\temp\sampleDelimitedFile.txt" traceOutputOptions="DateTime" /> <add name="XmlWriter" type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" initializeData="c:\temp\sampleLogFile.xml" /> <add name="Console" type="System.Diagnostics.ConsoleTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" initializeData="true" /> </sharedListeners> </system.diagnostics> </configuration>
To reconfigure a listener
Locate the listener's
<add>
element from the<sharedListeners>
section.The
type
attribute gives the name of the listener type. This type must inherit from the TraceListener class. Use the strongly named type name to ensure that the right type is used. For more information, see the "To reference a strongly named type" section below.Some types that you can use are:
A Microsoft.VisualBasic.Logging.FileLogTraceListener listener, which writes to a file log.
A System.Diagnostics.EventLogTraceListener listener, which writes information to the computer event log specified by the
initializeData
parameter.The System.Diagnostics.DelimitedListTraceListener and System.Diagnostics.XmlWriterTraceListener listeners, which write to the file specified in the
initializeData
parameter.A System.Diagnostics.ConsoleTraceListener listener, which writes to the command-line console.
For information about where other types of log listeners write information, consult that type's documentation.
When the application creates the log-listener object, it passes the
initializeData
attribute as the constructor parameter. The meaning of theinitializeData
attribute depends on the trace listener.After creating the log listener, the application sets the listener's properties. These properties are defined by the other attributes in the
<add>
element. For more information on the properties for a particular listener, see the documentation for that listener's type.
To reference a strongly named type
To ensure that the right type is used for your log listener, make sure to use the fully qualified type name and the strongly named assembly name. The syntax of a strongly named type is as follows:
<type name>, <assembly name>, <version number>, <culture>, <strong name>
This code example shows how to determine the strongly named type name for a fully qualified type—"System.Diagnostics.FileLogTraceListener" in this case.
Public Sub DisplayStrongName() Dim t As Type = GetType(Logging.FileLogTraceListener) MsgBox(t.FullName & ", " & t.Assembly.FullName) End Sub
This is the output, and it can be used to uniquely reference a strongly named type, as in the "To add listeners" procedure above.
Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a