日誌偵聽器

listen listen Listen

可以使用 My.Application.LogMy.Log 對象記錄有關應用程式中發生的事件的信息。
Log 對象可以使用日誌篩選來限制它所記錄的信息量。如果篩選配置不正確,則日誌可能包含錯誤的信息。
但是,如果日誌配置不正確,則可能需要有關其當前配置的更多信息。可以通過日誌的 TraceSource 高級屬性獲取此信息。
為代碼中的日誌對象確定日誌偵聽器
1.在代碼檔案的開頭導入 System.Diagnostics 命名空間。
Visual Basic
Imports System.Diagnostics
2.創建一個函式,用於返回由每個日誌偵聽器的信息組成的字元串。
Visual Basic
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.將日誌的跟蹤偵聽器集合傳遞給 GetListeners 函式,並顯示返回值。
Visual Basic
Dim ListenerCollection As TraceListenerCollection
ListenerCollection = My.Application.Log.TraceSource.Listeners
Dim ListenersText As String = GetListeners(ListenerCollection)
MsgBox(ListenersText)
有關更多信息,請參見 TraceSource 屬性(My.Application.Log 和 My.Log)。

相關詞條

熱門詞條

聯絡我們