更新:2007 年 11 月
将一条消息写入应用程序的日志侦听器。
' Usage
My.Application.Log.WriteEntry(message)
My.Application.Log.WriteEntry(message ,severity)
My.Application.Log.WriteEntry(message ,severity ,id)
' Web usage
My.Log.WriteEntry(message)
My.Log.WriteEntry(message ,severity)
My.Log.WriteEntry(message ,severity ,id)
' Declaration
Public Sub WriteEntry( _
   ByVal message As String _
)
' -or-
Public Sub WriteEntry( _
   ByVal message As String, _
   ByVal severity As System.Diagnostics.TraceEventType _
)
' -or-
Public Sub WriteEntry( _
   ByVal message As String, _
   ByVal severity As System.Diagnostics.TraceEventType, _
   ByVal id As Integer _
)
参数
- message 
 必选。要记录的消息。如果 message 为 Nothing,则使用空字符串。
- severity 
 消息的类型。默认情况下为 TraceEventType.Information。
- id 
 消息标识符,通常用于建立关联。默认情况下与 entryType 相关,如表中所述。
异常
下面的情况会导致异常:
- 异常或消息类型为 Nothing (ArgumentNullException)。 
- 消息类型不是 TraceEventType 枚举值之一 (InvalidEnumArgumentException)。 
- 部分受信任的代码调用了方法,但写入了需要完全信任仅限的事件日志侦听器 (SecurityException)。 
备注
WriteEntry 方法将消息写入应用程序的事件日志侦听器。
在客户端应用程序中,Log 对象是通过 My.Application.Log 对象提供的。在 Web 应用程序中,Log 对象是通过 My.Log 对象提供的。
若要了解哪些日志侦听器接收 WriteEntry 方法的消息,请参见演练:确定 My.Application.Log 写入信息的位置。可以更改默认日志侦听器。有关更多信息,请参见使用 Application 日志 (Visual Basic)。
对于不接受 id 参数的重载,写入日志的 id 是由下表定义的。
| severity | 默认 id | 
|---|---|
| 0 | |
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | 
任务
下表列出了涉及 WriteEntry 方法的任务的示例。
| 要执行的操作 | 请参见 | 
|---|---|
| 将事件信息写入应用程序的日志侦听器 | |
| 确定 Log 在何处写入信息 | 
示例
此示例说明如何使用 My.Application.Log.WriteEntry 方法来记录跟踪信息。有关更多信息,请参见如何:写入日志消息。
Public Sub TracingTest(ByVal fileName As String)
    My.Application.Log.WriteEntry( _
        "Entering TracingTest with argument " & _
        fileName & ".")
    ' Code to trace goes here.
    My.Application.Log.WriteEntry( _
        "Exiting TracingTest with argument " & _
        fileName & ".")
End Sub
此代码示例只能在客户端应用程序内运行。对于 Web 应用程序,请将 My.Application.Log.WriteEntry 改为 My.Log.WriteEntry。
要求
命名空间:Microsoft.VisualBasic.Logging
类:Log
**程序集:**Visual Basic 运行库(在 Microsoft.VisualBasic.dll 中)
按项目类型列出的可用性
| 项目类型 | 可用 | 
|---|---|
| Windows 应用程序 | 是 | 
| 类库 | 是 | 
| 控制台应用程序 | 是 | 
| Windows 控件库 | 是 | 
| Web 控件库 | 否 | 
| Windows 服务 | 是 | 
| 网站 | 是 | 
权限
以下权限可能是必需的:
| 权限 | 说明 | 
|---|---|
| 控制访问文件和文件夹的能力。关联的枚举:Unrestricted。 | 
请参见
任务
演练:确定 My.Application.Log 写入信息的位置
概念
使用 Application 日志 (Visual Basic)