Log.WriteEntry 方法

定义

将消息写入应用程序的日志侦听器。

重载

名称 说明
WriteEntry(String)

将消息写入应用程序的日志侦听器。

WriteEntry(String, TraceEventType)

将消息写入应用程序的日志侦听器。

WriteEntry(String, TraceEventType, Int32)

将消息写入应用程序的日志侦听器。

WriteEntry(String)

Source:
Log.vb
Source:
Log.vb
Source:
Log.vb
Source:
Log.vb
Source:
Log.vb

将消息写入应用程序的日志侦听器。

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 Web 应用程序。

注解

该方法 WriteEntry 将消息写入应用程序的事件日志侦听器。

在客户端应用程序中,该 Log 对象可通过 My.Application.Log 该对象获得。 在 Web 应用程序中,该 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 写入信息的位置

按项目类型提供可用性

项目类型 可用的
Windows 应用程序 Yes
类库 Yes
控制台应用程序 Yes
Windows 控件库 Yes
Web 控件库
Windows 服务 Yes
网站 Yes

另请参阅

适用于

WriteEntry(String, TraceEventType)

Source:
Log.vb
Source:
Log.vb
Source:
Log.vb
Source:
Log.vb
Source:
Log.vb

将消息写入应用程序的日志侦听器。

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 Web 应用程序。

注解

该方法 WriteEntry 将消息写入应用程序的事件日志侦听器。

在客户端应用程序中,该 Log 对象可通过 My.Application.Log 该对象获得。 在 Web 应用程序中,该 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 写入信息的位置

按项目类型提供可用性

项目类型 可用的
Windows 应用程序 Yes
类库 Yes
控制台应用程序 Yes
Windows 控件库 Yes
Web 控件库
Windows 服务 Yes
网站 Yes

另请参阅

适用于

WriteEntry(String, TraceEventType, Int32)

Source:
Log.vb
Source:
Log.vb
Source:
Log.vb
Source:
Log.vb
Source:
Log.vb

将消息写入应用程序的日志侦听器。

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 Web 应用程序。

注解

该方法 WriteEntry 将消息写入应用程序的事件日志侦听器。

在客户端应用程序中,该 Log 对象可通过 My.Application.Log 该对象获得。 在 Web 应用程序中,该 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 写入信息的位置

按项目类型提供可用性

项目类型 可用的
Windows 应用程序 Yes
类库 Yes
控制台应用程序 Yes
Windows 控件库 Yes
Web 控件库
Windows 服务 Yes
网站 Yes

另请参阅

适用于