Log.WriteEntry 方法

定義

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

多載

名稱 Description
WriteEntry(String)

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

WriteEntry(String, TraceEventType)

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

WriteEntry(String, TraceEventType, Int32)

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

WriteEntry(String)

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

public:
 void WriteEntry(System::String ^ message);
public void WriteEntry(string message);
member this.WriteEntry : string -> unit
Public Sub WriteEntry (message As String)

參數

message
String

必須的。 要記錄的訊息。 若 message 為 , Nothing則使用空字串。

例外狀況

部分信任的程式碼呼叫該方法,但寫入事件日誌監聽器時需要完全信任。

範例

此範例展示了如何使用此 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.WriteEntryMy.Log.WriteEntry for for Web 應用程式。

備註

WriteEntry 方法會寫入訊息給應用程式的事件日誌監聽器。

在用戶端應用程式中, Log 物件是透過物件 My.Application.Log 取得的。 在網頁應用程式中, Log 物件是透過物件 My.Log 來取得的。

要了解哪些日誌監聽器接收該 WriteEntry 方法的訊息,請參閱「 攻略:確定 My.Application.Log 在哪裡寫入資訊」。 你可以更改預設的日誌監聽器。 欲了解更多資訊,請參閱 「應用日誌操作」。

對於不包含參數的 id 超載,寫入日誌的定義 id 如下表。

severity 默認 id
Information 0
Warning 1
Error 2
Critical 3
Start 4
Stop 5
Suspend 6
Resume 7
Verbose 8
Transfer 9

下表列出與此 WriteEntry 方法相關的任務範例。

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

依專案類型提供可用性

專案類型 Available
Windows 應用程式 Yes
類別庫 Yes
主控台應用程式 Yes
Windows 控件庫 Yes
Web 控制庫 No
Windows 服務 Yes
網站 Yes

另請參閱

適用於

WriteEntry(String, TraceEventType)

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

public:
 void WriteEntry(System::String ^ message, System::Diagnostics::TraceEventType severity);
public void WriteEntry(string message, System.Diagnostics.TraceEventType severity);
member this.WriteEntry : string * System.Diagnostics.TraceEventType -> unit
Public Sub WriteEntry (message As String, severity As TraceEventType)

參數

message
String

必須的。 要記錄的訊息。 若 message 為 , Nothing則使用空字串。

severity
TraceEventType

訊息類型。 根據預設,TraceEventType.Information

例外狀況

訊息類型並非 TraceEventType 列舉值之一。

部分信任的程式碼呼叫該方法,但寫入事件日誌監聽器時需要完全信任。

範例

此範例展示了如何使用此 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.WriteEntryMy.Log.WriteEntry for for Web 應用程式。

備註

WriteEntry 方法會寫入訊息給應用程式的事件日誌監聽器。

在用戶端應用程式中, Log 物件是透過物件 My.Application.Log 取得的。 在網頁應用程式中, Log 物件是透過物件 My.Log 來取得的。

要了解哪些日誌監聽器接收該 WriteEntry 方法的訊息,請參閱「 攻略:確定 My.Application.Log 在哪裡寫入資訊」。 你可以更改預設的日誌監聽器。 欲了解更多資訊,請參閱 「應用日誌操作」。

對於不包含參數的 id 超載,寫入日誌的定義 id 如下表。

severity 默認 id
Information 0
Warning 1
Error 2
Critical 3
Start 4
Stop 5
Suspend 6
Resume 7
Verbose 8
Transfer 9

下表列出與此 WriteEntry 方法相關的任務範例。

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

依專案類型提供可用性

專案類型 Available
Windows 應用程式 Yes
類別庫 Yes
主控台應用程式 Yes
Windows 控件庫 Yes
Web 控制庫 No
Windows 服務 Yes
網站 Yes

另請參閱

適用於

WriteEntry(String, TraceEventType, Int32)

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

public:
 void WriteEntry(System::String ^ message, System::Diagnostics::TraceEventType severity, int id);
public void WriteEntry(string message, System.Diagnostics.TraceEventType severity, int id);
member this.WriteEntry : string * System.Diagnostics.TraceEventType * int -> unit
Public Sub WriteEntry (message As String, severity As TraceEventType, id As Integer)

參數

message
String

必須的。 要記錄的訊息。 若 message 為 , Nothing則使用空字串。

severity
TraceEventType

訊息類型。 根據預設,TraceEventType.Information

id
Int32

訊息識別碼,通常用於相關性。 預設情況下,與表格中所述相關。entryType

例外狀況

訊息類型並非 TraceEventType 列舉值之一。

部分信任的程式碼呼叫該方法,但寫入事件日誌監聽器時需要完全信任。

範例

此範例展示了如何使用此 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.WriteEntryMy.Log.WriteEntry for for Web 應用程式。

備註

WriteEntry 方法會寫入訊息給應用程式的事件日誌監聽器。

在用戶端應用程式中, Log 物件是透過物件 My.Application.Log 取得的。 在網頁應用程式中, Log 物件是透過物件 My.Log 來取得的。

要了解哪些日誌監聽器接收該 WriteEntry 方法的訊息,請參閱「 攻略:確定 My.Application.Log 在哪裡寫入資訊」。 你可以更改預設的日誌監聽器。 欲了解更多資訊,請參閱 「應用日誌操作」。

對於不包含參數的 id 超載,寫入日誌的定義 id 如下表。

severity 默認 id
Information 0
Warning 1
Error 2
Critical 3
Start 4
Stop 5
Suspend 6
Resume 7
Verbose 8
Transfer 9

下表列出與此 WriteEntry 方法相關的任務範例。

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

依專案類型提供可用性

專案類型 Available
Windows 應用程式 Yes
類別庫 Yes
主控台應用程式 Yes
Windows 控件庫 Yes
Web 控制庫 No
Windows 服務 Yes
網站 Yes

另請參閱

適用於