Log 類別

定義

提供一個屬性與方法,用於將事件與異常資訊寫入應用程式的日誌監聽器。

public ref class Log
public class Log
type Log = class
Public Class Log
繼承
Log
衍生

範例

此範例展示了如何使用此 My.Application.Log.WriteEntry 方法記錄追蹤資訊。 欲了解更多資訊,請參閱 「如何撰寫日誌訊息」。

Private Sub GetOpenFormTitles()
    Dim formTitles As New Collection

    Try
        For Each f As Form In My.Application.OpenForms
            ' Use a thread-safe method to get all form titles.
            formTitles.Add(GetFormTitle(f))
        Next
    Catch ex As Exception
        formTitles.Add("Error: " & ex.Message)
    End Try

    Form1.ListBox1.DataSource = formTitles
End Sub

Private Delegate Function GetFormTitleDelegate(f As Form) As String
Private Function GetFormTitle(f As Form) As String
    ' Check if the form can be accessed from the current thread.
    If Not f.InvokeRequired Then
        ' Access the form directly.
        Return f.Text
    Else
        ' Marshal to the thread that owns the form. 
        Dim del As GetFormTitleDelegate = AddressOf GetFormTitle
        Dim param As Object() = {f}
        Dim result As System.IAsyncResult = f.BeginInvoke(del, param)
        ' Give the form's thread a chance process function.
        System.Threading.Thread.Sleep(10)
        ' Check the result.
        If result.IsCompleted Then
            ' Get the function's return value.
            Return "Different thread: " & f.EndInvoke(result).ToString
        Else
            Return "Unresponsive thread"
        End If
    End If
End Function

備註

My.Application.Log 物件提供了一個直接的入口,方便存取 .NET Framework 的日誌服務。 WriteEntryWriteException 方法會將訊息寫入應用程式的日誌監聽器。 監聽器可由應用程式的設定檔設定。 欲了解更多資訊,請參閱 攻略:變更 My.Application.Log 在哪裡寫入資訊處理應用程式日誌

My.Application.Log 物件僅提供給用戶端應用程式使用。 對於網頁應用程式,請使用 My.Log. 如需詳細資訊,請參閱AspLog

下表列出涉及該 My.Application.Log 物件的任務範例。

看!
將事件資訊寫入應用程式的日誌監聽器 如何:寫入記錄訊息
將異常資訊寫入應用程式的日誌監聽器 如何:記錄例外狀況
判斷資訊寫入位置My.Application.Log 操作指南:判斷 My.Application.Log 寫入資訊的位置

建構函式

名稱 Description
Log()

初始化 Log 類別的新執行個體。

Log(String)

初始化 Log 類別的新執行個體。

屬性

名稱 Description
DefaultFileLogWriter

取得該Log物件底層的檔案FileLogTraceListener

TraceSource

能接觸 TraceSource 到該物件底下的 Log 物件。

方法

名稱 Description
Equals(Object)

判斷指定的 物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type

(繼承來源 Object)
InitializeWithDefaultsSinceNoConfigExists()

建立一個新FileLogTraceListener物件並加入收藏。Listeners

MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)
WriteEntry(String, TraceEventType, Int32)

會寫訊息給應用程式的日誌監聽器。

WriteEntry(String, TraceEventType)

會寫訊息給應用程式的日誌監聽器。

WriteEntry(String)

會寫訊息給應用程式的日誌監聽器。

WriteException(Exception, TraceEventType, String, Int32)

將例外資訊寫入應用程式的日誌監聽器。

WriteException(Exception, TraceEventType, String)

將例外資訊寫入應用程式的日誌監聽器。

WriteException(Exception)

將例外資訊寫入應用程式的日誌監聽器。

適用於

另請參閱