EventLogTraceListener Class

Definition

提供一个简单的侦听器,该侦听器将跟踪或调试输出指引至 EventLogProvides a simple listener that directs tracing or debugging output to an EventLog.

public ref class EventLogTraceListener sealed : System::Diagnostics::TraceListener
public sealed class EventLogTraceListener : System.Diagnostics.TraceListener
type EventLogTraceListener = class
    inherit TraceListener
Public NotInheritable Class EventLogTraceListener
Inherits TraceListener
Inheritance
EventLogTraceListener

Examples

下面的示例创建一个将输出发送到事件日志的跟踪侦听器。The following example creates a trace listener that sends output to an event log. 首先,该代码创建一个使用源 myEventLogSource的新 EventLogTraceListenerFirst, the code creates a new EventLogTraceListener that uses the source myEventLogSource. 接下来,myTraceListener 添加到 Trace.Listeners 集合。Next, myTraceListener is added to the Trace.Listeners collection. 最后,该示例将一个输出行发送到 Listeners 的对象。Finally, the example sends a line of output to the Listeners object.

int main()
{
   #if defined(TRACE)
   
   // Create a trace listener for the event log.
   EventLogTraceListener^ myTraceListener = 
      gcnew EventLogTraceListener( "myEventLogSource" );
   
   // Add the event log trace listener to the collection.
   Trace::Listeners->Add( myTraceListener );
   
   // Write output to the event log.
   Trace::WriteLine( "Test output" );
   
   #endif
}
public static void Main(string[] args) {

    // Create a trace listener for the event log.
    EventLogTraceListener myTraceListener = new EventLogTraceListener("myEventLogSource");

    // Add the event log trace listener to the collection.
    Trace.Listeners.Add(myTraceListener);

    // Write output to the event log.
    Trace.WriteLine("Test output");
 }

Overloads Public Shared Sub Main(args() As String)
   
    ' Create a trace listener for the event log.
    Dim myTraceListener As New EventLogTraceListener("myEventLogSource")
    
    ' Add the event log trace listener to the collection.
    Trace.Listeners.Add(myTraceListener)
    
    ' Write output to the event log.
    Trace.WriteLine("Test output")
End Sub

Remarks

EventLogTraceListener 的实例可以添加到 Debug.ListenersTrace.ListenersTraceSource.Listeners 集合,以将跟踪或调试的输出发送到 EventLogAn instance of this EventLogTraceListener can be added to the Debug.Listeners, Trace.Listeners, or TraceSource.Listeners collections to send output from tracing or debugging to an EventLog. 您可以在代码中添加该实例,或在应用程序配置文件中指定它。You can add the instance in your code or specify it in the application configuration file. 若要使用应用程序配置文件添加 EventLogTraceListener,请在 Visual Studio 2005Visual Studio 2005 项目中编辑对应于应用程序名称或 app.config 文件的文件。To add an EventLogTraceListener using an application configuration file, edit the file that corresponds to the name of your application or the app.config file in a Visual Studio 2005Visual Studio 2005 project. 在此文件中,为 System.Diagnostics.EventLogTraceListener 类型插入元素。In this file, insert an element for a System.Diagnostics.EventLogTraceListener type.

下面的示例将名为 myListenerEventLogTraceListener 对象添加到 Trace.Listeners 集合。The following example adds a EventLogTraceListener object named myListener to the Trace.Listeners collection. initializeData 参数指定要传递到 EventLogTraceListener(String) 构造函数的事件日志源的名称。The initializeData parameter specifies the name of the event log source that is to be passed to the EventLogTraceListener(String) constructor.

<configuration>  
  <system.diagnostics>  
    <trace autoflush="false" indentsize="4">  
      <listeners>  
        <add name="myListener"  
          type="System.Diagnostics.EventLogTraceListener"  
          initializeData="TraceListenerLog" />  
      </listeners>  
    </trace>  
  </system.diagnostics>  
</configuration>  

有关如何在应用程序配置文件中添加跟踪侦听器的详细信息,请参阅或。For more information about how to add trace listeners in the application configuration file, see or .

Note

如果与 EventLogTraceListener 实例关联的事件日志的 Source 不存在,则将创建一个新的事件源。If the Source for the event log that is associated with the EventLogTraceListener instance does not exist, a new event source is created. 若要在 Windows Vista、Windows XP Professional 或 Windows Server 2003 中创建事件源,您必须拥有管理特权。To create an event source in Windows Vista, Windows XP Professional, or Windows Server 2003, you must have administrative privileges.

此要求的原因是必须搜索所有事件日志(包括安全性),以确定事件源是否唯一。The reason for this requirement is that all event logs, including security, must be searched to determine whether the event source is unique. 在 Windows Vista 中,用户无权访问安全日志;因此将引发 SecurityExceptionIn Windows Vista, users do not have permission to access the security log; therefore, a SecurityException is thrown.

在 Windows Vista 中,用户帐户控制 (UAC) 决定用户的特权。In Windows Vista, User Account Control (UAC) determines the privileges of a user. 如果您是内置的 Administrators 组的成员,将为您分配两个运行时访问令牌:一个标准用户访问令牌和一个管理员访问令牌。If you are a member of the Built-in Administrators group, you are assigned two run-time access tokens: a standard user access token and an administrator access token. 默认情况下,您拥有标准用户角色。By default, you are in the standard user role. 若要执行访问安全日志的代码,必须首先将你的特权从标准用户提升到管理员。To execute the code that accesses the security log, you must first elevate your privileges from standard user to administrator. 你可以通过以下方式执行此操作:右键单击应用程序图标并指示需以管理员身份运行。You can do this when you start an application by right-clicking the application icon and indicating that you want to run as an administrator.

类提供 EventLog 属性,用于获取或设置接收跟踪或调试输出的事件日志,以及用于保存 EventLogTraceListener名称的 Name 属性。The class provides the EventLog property to get or set the event log that receives the tracing or debugging output, and the Name property to hold the name of the EventLogTraceListener.

Close 方法会关闭事件日志,使其不再接收跟踪或调试输出。The Close method closes the event log so it no longer receives tracing or debugging output. WriteWriteLine 方法将消息写入事件日志。The Write and WriteLine methods write a message to the event log.

Note

为了避免向事件日志写入大量数据的可能性,EventLogTraceListener 不会输出 TraceOutputOptions 属性指定的可选跟踪数据。To avoid the possibility of writing large amounts of data to the event log, the EventLogTraceListener does not output the optional trace data specified by the TraceOutputOptions property.

Constructors

EventLogTraceListener()

在没有跟踪侦听器的情况下,初始化 EventLogTraceListener 类的新实例。Initializes a new instance of the EventLogTraceListener class without a trace listener.

EventLogTraceListener(EventLog)

使用指定的事件日志初始化 EventLogTraceListener 类的新实例。Initializes a new instance of the EventLogTraceListener class using the specified event log.

EventLogTraceListener(String)

使用指定的源初始化 EventLogTraceListener 类的新实例。Initializes a new instance of the EventLogTraceListener class using the specified source.

Properties

Attributes

获取应用程序配置文件中定义的自定义跟踪侦听器特性。Gets the custom trace listener attributes defined in the application configuration file.

(Inherited from TraceListener)
EventLog

获取或设置要写入的事件日志。Gets or sets the event log to write to.

Filter

获取或设置跟踪侦听器的跟踪筛选器。Gets or sets the trace filter for the trace listener.

(Inherited from TraceListener)
IndentLevel

获取或设置缩进级别。Gets or sets the indent level.

(Inherited from TraceListener)
IndentSize

获取或设置缩进的空格数。Gets or sets the number of spaces in an indent.

(Inherited from TraceListener)
IsThreadSafe

获取一个值,该值指示跟踪侦听器是否是线程安全的。Gets a value indicating whether the trace listener is thread safe.

(Inherited from TraceListener)
Name

获取或设置此 EventLogTraceListener 的名称。Gets or sets the name of this EventLogTraceListener.

NeedIndent

获取或设置一个值,该值指示是否缩进输出。Gets or sets a value indicating whether to indent the output.

(Inherited from TraceListener)
TraceOutputOptions

获取或设置跟踪输出选项。Gets or sets the trace output options.

(Inherited from TraceListener)

Methods

Close()

关闭事件日志,以使其不再接收跟踪或调试输出。Closes the event log so that it no longer receives tracing or debugging output.

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Dispose()

释放 TraceListener 使用的所有资源。Releases all resources used by the TraceListener.

(Inherited from TraceListener)
Dispose(Boolean)

释放 TraceListener 使用的非托管资源,并选择性地释放托管资源。Releases the unmanaged resources used by the TraceListener and optionally releases the managed resources.

(Inherited from TraceListener)
Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
Fail(String)

实现 TraceListener 类时,向所创建的侦听器发出错误信息。Emits an error message to the listener you create when you implement the TraceListener class.

(Inherited from TraceListener)
Fail(String, String)

实现 TraceListener 类时,向所创建的侦听器发出错误信息和详细错误信息。Emits an error message and a detailed error message to the listener you create when you implement the TraceListener class.

(Inherited from TraceListener)
Flush()

在派生类中被重写时,刷新输出缓冲区。When overridden in a derived class, flushes the output buffer.

(Inherited from TraceListener)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetSupportedAttributes()

获取跟踪侦听器支持的自定义特性。Gets the custom attributes supported by the trace listener.

(Inherited from TraceListener)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)
TraceData(TraceEventCache, String, TraceEventType, Int32, Object)

将跟踪信息、数据对象和事件信息写入事件日志中。Writes trace information, a data object, and event information to the event log.

TraceData(TraceEventCache, String, TraceEventType, Int32, Object)

向特定于侦听器的输出中写入跟踪信息、数据对象和事件信息。Writes trace information, a data object and event information to the listener specific output.

(Inherited from TraceListener)
TraceData(TraceEventCache, String, TraceEventType, Int32, Object[])

将跟踪信息、数据对象的数组和事件信息写入事件日志中。Writes trace information, an array of data objects, and event information to the event log.

TraceData(TraceEventCache, String, TraceEventType, Int32, Object[])

向特定于侦听器的输出中写入跟踪信息、数据对象的数组和事件信息。Writes trace information, an array of data objects and event information to the listener specific output.

(Inherited from TraceListener)
TraceEvent(TraceEventCache, String, TraceEventType, Int32)

向特定于侦听器的输出写入跟踪和事件信息。Writes trace and event information to the listener specific output.

(Inherited from TraceListener)
TraceEvent(TraceEventCache, String, TraceEventType, Int32, String)

将跟踪信息、消息和事件信息写入事件日志中。Writes trace information, a message, and event information to the event log.

TraceEvent(TraceEventCache, String, TraceEventType, Int32, String)

向特定于侦听器的输出中写入跟踪信息、消息和事件信息。Writes trace information, a message, and event information to the listener specific output.

(Inherited from TraceListener)
TraceEvent(TraceEventCache, String, TraceEventType, Int32, String, Object[])

将跟踪信息、格式化对象数组和事件信息写入事件日志中。Writes trace information, a formatted array of objects, and event information to the event log.

TraceEvent(TraceEventCache, String, TraceEventType, Int32, String, Object[])

向特定于侦听器的输出中写入跟踪信息、格式化对象数组和事件信息。Writes trace information, a formatted array of objects and event information to the listener specific output.

(Inherited from TraceListener)
TraceTransfer(TraceEventCache, String, Int32, String, Guid)

向侦听器特定的输出中写入跟踪信息、消息、相关活动标识和事件信息。Writes trace information, a message, a related activity identity and event information to the listener specific output.

(Inherited from TraceListener)
Write(Object)

实现 ToString() 类时,向所创建的侦听器写入对象的 TraceListener 方法值。Writes the value of the object's ToString() method to the listener you create when you implement the TraceListener class.

(Inherited from TraceListener)
Write(Object, String)

实现 ToString() 类时,向所创建的侦听器写入类别名称和对象的 TraceListener 方法值。Writes a category name and the value of the object's ToString() method to the listener you create when you implement the TraceListener class.

(Inherited from TraceListener)
Write(String)

将消息写入此实例的事件日志。Writes a message to the event log for this instance.

Write(String, String)

实现 TraceListener 类时,向所创建的侦听器写入类别名称和消息。Writes a category name and a message to the listener you create when you implement the TraceListener class.

(Inherited from TraceListener)
WriteIndent()

实现此类时,向所创建的侦听器写入缩进,并将 NeedIndent 属性重置为 falseWrites the indent to the listener you create when you implement this class, and resets the NeedIndent property to false.

(Inherited from TraceListener)
WriteLine(Object)

实现 TraceListener 类时,向所创建的侦听器写入对象的 ToString() 方法值,后跟行结束符。Writes the value of the object's ToString() method to the listener you create when you implement the TraceListener class, followed by a line terminator.

(Inherited from TraceListener)
WriteLine(Object, String)

实现 TraceListener 类时,向所创建的侦听器写入类别名称和对象的 ToString() 方法值,后跟行结束符。Writes a category name and the value of the object's ToString() method to the listener you create when you implement the TraceListener class, followed by a line terminator.

(Inherited from TraceListener)
WriteLine(String)

将消息写入此实例的事件日志。Writes a message to the event log for this instance.

WriteLine(String, String)

实现 TraceListener 类时,向所创建的侦听器写入类别名称和消息,后跟行结束符。Writes a category name and a message to the listener you create when you implement the TraceListener class, followed by a line terminator.

(Inherited from TraceListener)

Applies to

Thread Safety

此类型是线程安全的。This type is thread safe.

See also