Walkthrough: Creating Custom Log Listeners (Visual Basic)
This walkthrough demonstrates how to create a custom log listener and configure it to listen to the output of the My.Application.Log object.
Log listeners must inherit from the TraceListener class.
To create the listener
In your application, create a class named SimpleListener that inherits from TraceListener.
Public Class SimpleListener Inherits System.Diagnostics.TraceListener <Security.Permissions.HostProtection(Synchronization:=True)> Public Overloads Overrides Sub Write(ByVal message As String) MsgBox("Write: " & message) End Sub <Security.Permissions.HostProtection(Synchronization:=True)> Public Overloads Overrides Sub WriteLine(ByVal message As String) MsgBox("WriteLine: " & message) End Sub End Class
The HostProtectionAttribute attribute is applied to the Write and WriteLine methods so that their attributes match the base class methods. The HostProtectionAttribute attribute allows the host that runs the code to determine that the code exposes host-protection synchronization.
The HostProtectionAttribute attribute is effective only on unmanaged applications that host the common language runtime and that implement host protection, such as SQL Server.
To ensure that My.Application.Log uses your log listener, you should strongly name the assembly that contains your log listener.
The next procedure provides some simple steps for creating a strongly named log-listener assembly. For more information, see Creating and Using Strong-Named Assemblies.
To strongly name the log-listener assembly
Have a project selected in Solution Explorer. On the Project menu, choose Properties. For more information, see Introduction to the Project Designer.
Click the Signing tab.
Select the Sign the assembly box.
Select <New> from the Choose a strong name key file drop-down list.
The Create Strong Name Key dialog box opens.
Provide a name for the key file in the Key file name box.
Enter a password in the Enter password and Confirm password boxes.
Rebuild the application.
Now that the assembly has a strong name, you need to determine the strong name of the listener so that My.Application.Log uses your log listener.
The format of a strongly named type is as follows.
<type name>, <assembly name>, <version number>, <culture>, <strong name>
To determine the strong name of the listener
With the strong name, you can add the listener to the My.Application.Log log-listener collection.
To add the listener to My.Application.Log
Right-click on app.config in the Solution Explorer and choose Open.
If there is an app.config file:
On the Project menu, choose Add New Item.
From the Add New Item dialog box, choose Application Configuration File.
Locate the <listeners> section, in the <source> section with the name attribute "DefaultSource", located in the <sources> section. The <sources> section is located in the <system.diagnostics> section, in the top-level <configuration> section.
Add this element to the <listeners> section:
<add name="SimpleLog" />
Locate the <sharedListeners> section, in the <system.diagnostics> section, in the top-level <configuration> section.
Add this element to that <sharedListeners> section:
<add name="SimpleLog" type="SimpleLogStrongName" />
Change the value of SimpleLogStrongName to be the strong name of the listener.