Troubleshooting: Log Listeners (Visual Basic)

You can use the My.Application.Log and My.Log objects to log information about events that occur in your application.

To determine which log listeners receive those messages, see Walkthrough: Determining Where My.Application.Log Writes Information (Visual Basic).

The Log object can use log filtering to limit the amount of information that it logs. If the filters are misconfigured, the logs might contain the wrong information. For more information about filtering, see Walkthrough: Filtering My.Application.Log Output (Visual Basic).

However, if a log is configured incorrectly, you may need more information about its current configuration. You can get to this information through the log's advanced TraceSource property.

To determine the log listeners for the Log object in code

  1. Import the System.Diagnostics namespace at the beginning of the code file. For more information, see Imports Statement (.NET Namespace and Type).

    Imports System.Diagnostics
    
  2. Create a function that returns a string consisting of information for each of the log's listeners.

    Function GetListeners(ByVal listeners As TraceListenerCollection) As String 
        Dim ret As String = "" 
        For Each listener As TraceListener In listeners
            ret &= listener.Name
            Dim listenerType As Type = listener.GetType 
            If listenerType Is GetType(DefaultTraceListener) Then 
                Dim tmp As DefaultTraceListener = 
                    DirectCast(listener, DefaultTraceListener)
                ret &= ": Writes to the debug output." 
            ElseIf listenerType Is GetType(Logging.FileLogTraceListener) Then 
                Dim tmp As Logging.FileLogTraceListener = 
                    DirectCast(listener, Logging.FileLogTraceListener)
                ret &= ": Log filename: " & tmp.FullLogFileName
            ElseIf listenerType Is GetType(EventLogTraceListener) Then 
                Dim tmp As EventLogTraceListener = 
                    DirectCast(listener, EventLogTraceListener)
                ret &= ": Event log name: " & tmp.EventLog.Log
            ElseIf listenerType Is GetType(XmlWriterTraceListener) Then 
                Dim tmp As Diagnostics.XmlWriterTraceListener = 
                    DirectCast(listener, XmlWriterTraceListener)
                ret &= ": XML log" 
            ElseIf listenerType Is GetType(ConsoleTraceListener) Then 
                Dim tmp As ConsoleTraceListener = 
                    DirectCast(listener, ConsoleTraceListener)
                ret &= ": Console log" 
            ElseIf listenerType Is GetType(DelimitedListTraceListener) Then 
                Dim tmp As DelimitedListTraceListener = 
                    DirectCast(listener, DelimitedListTraceListener)
                ret &= ": Delimited log" 
            Else
                ret &= ": Unhandeled log type: " & 
                    listenerType.ToString
            End If
            ret &= vbCrLf
        Next 
    
        Return ret
    End Function
    
  3. Pass the collection of the log's trace listeners to the GetListeners function, and display the return value.

    Dim ListenerCollection As TraceListenerCollection
    ListenerCollection = My.Application.Log.TraceSource.Listeners
    Dim ListenersText As String = GetListeners(ListenerCollection)
    MsgBox(ListenersText)
    

    For more information, see TraceSource.

See Also

Tasks

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

Reference

Log

Concepts

Working with Application Logs in Visual Basic