Share via


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.

Getting Started

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 Write and WriteLine methods, required by the base class, call MsgBox to display their input.

    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.

    Note

    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

  1. Have a project selected in Solution Explorer. On the Project menu, choose Properties. For more information, see Introduction to the Project Designer.

  2. Click the Signing tab.

  3. Select the Sign the assembly box.

  4. Select <New> from the Choose a strong name key file drop-down list.

    The Create Strong Name Key dialog box opens.

  5. Provide a name for the key file in the Key file name box.

  6. Enter a password in the Enter password and Confirm password boxes.

  7. Click OK.

  8. Rebuild the application.

Adding the Listener

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

  • The following code shows how to determine the strongly named type name for SimpleListener.

    Public Sub DisplaySimpleListenerStrongName()
        Dim t As Type = GetType(SimpleListener)
        MsgBox(t.FullName & ", " & t.Assembly.FullName)
    End Sub
    

    The strong name of the type depends on your project.

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

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

    -or-

    If there is an app.config file:

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

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

    3. Click Add.

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

  3. Add this element to the <listeners> section:

    <add name="SimpleLog" />
    
  4. Locate the <sharedListeners> section, in the <system.diagnostics> section, in the top-level <configuration> section.

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

See Also

Tasks

How to: Log Exceptions in Visual Basic

How to: Write Log Messages (Visual Basic)

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

Reference

Log

Concepts

Working with Application Logs in Visual Basic