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 的日志记录服务。 这些 WriteEntry 消息和 WriteException 方法将消息写入应用程序的日志侦听器。 侦听器可由应用程序的配置文件配置。 有关详细信息,请参阅演练:更改 My.Application.Log 写入信息和使用应用程序日志的位置。

My.Application.Log 对象仅适用于客户端应用程序。 对于 Web 应用程序,请使用 My.Log。 有关详细信息,请参阅 AspLog

下表列出了涉及 My.Application.Log 该对象的任务的示例。

收件人 请参阅
将事件信息写入应用程序的日志侦听器 如何:编写日志消息
将异常信息写入应用程序的日志侦听器 如何记录异常
确定写入信息的位置My.Application.Log 演练:确定 My.Application.Log 写入信息的位置

构造函数

名称 说明
Log()

初始化 Log 类的新实例。

Log(String)

初始化 Log 类的新实例。

属性

名称 说明
DefaultFileLogWriter

获取对象所依据FileLogTraceListener的对象的文件Log

TraceSource

获取对象 TraceSource 所依据 Log 的对象。

方法

名称 说明
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)

将异常信息写入应用程序的日志侦听器。

适用于

另请参阅