1. Herfried K. Wagner’s VB.Any
  2. .NET
  3. Frequently Asked Questions

Receiving debug and trace messages in a form by adding a listener

Receiving debug and trace messages in a form by adding a listener
<URL:https://dotnet.currifex.org/dotnet/faqs/debugtracelistener/en/>
----------------------------------------------------------------------------

Receiving debug and trace messages in a form by adding a listener

Debug and trace messages can be received in a form (or any other place) by
adding a custom listener.  The sample below contains the class
'ExtendedTraceListener' which can be used to capture these messages:

\\\
Imports System.Diagnostics

''' <summary>
'''   Raises events for trace messages.
''' </summary>
Public Class ExtendedTraceListener
    Inherits TraceListener

    ''' <summary>
    '''   Occurs when a trace message is received.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">
    '''   A <c>ReceiveEventArgs</c> that contains the event data.
    ''' </param>
    Public Event Receive(ByVal sender As Object, ByVal e As ReceiveEventArgs)

    ''' <summary>
    '''   Raises the <c>Receive</c> event.
    ''' </summary>
    ''' <param name="e">
    '''   A <c>ReceiveEventArgs</c> that contains the event data.
    ''' </param>
    Protected Sub OnReceive(ByVal e As ReceiveEventArgs)
        RaiseEvent Receive(Me, e)
    End Sub

    Public Overloads Overrides Sub Write(ByVal message As String)
        OnReceive(New ReceiveEventArgs(message))
    End Sub

    Public Overloads Overrides Sub WriteLine(ByVal message As String)
        OnReceive(New ReceiveEventArgs(message & ControlChars.NewLine))
    End Sub
End Class

''' <summary>
'''   Provides data for the <c>Receive</c> event.
''' </summary>
Public Class ReceiveEventArgs
    Inherits EventArgs

    Private m_Text As String

    ''' <summary>
    '''   Creates a new instance of <c>ReceiveEventArgs</c>.
    ''' </summary>
    ''' <param name="Text">
    '''   The initial value of the <c>Text</c> property.
    ''' </param>
    Public Sub New(ByVal Text As String)
        Me.Text = Text
    End Sub

    ''' <summary>
    '''   Gets or sets the text of the received message.
    ''' </summary>
    ''' <value>The text of the received message.</value>
    Public Property Text() As String
        Get
            Return m_Text
        End Get
        Set(ByVal Value As String)
            m_Text = Value
        End Set
    End Property
End Class
///

Usage:

\\\
Private WithEvents DebugListener As New ExtendedTraceListener()
.
.
.

' In the constructor.
Debug.Listeners.Add(DebugListener)
.
.
.

' Later in the code.
Debug.Write("dot")
Debug.Write("net")
Debug.WriteLineIf(True, "Ha ha...")
Debug.WriteLine("")
Debug.WriteLine("Hello World!")
Debug.WriteLine("Visual Basic .NET rocks!")
.
.
.
Private Sub DebugListener_Receive( _
    ByVal sender As Object, _
    ByVal e As ReceiveEventArgs _
) Handles DebugListener.Receive
    Me.TextBox1.AppendText(e.Text)
End Sub
///