System.Diagnostics.EventLogTraceListener Class

Provides a simple listener that directs tracing or debugging output to an System.Diagnostics.EventLog.

See Also: EventLogTraceListener Members


public sealed class EventLogTraceListener : TraceListener


An instance of this System.Diagnostics.EventLogTraceListener can be added to the Debug.Listeners, Trace.Listeners, or TraceSource.Listeners collections to send output from tracing or debugging to an System.Diagnostics.EventLog. You can add the instance in your code or specify it in the application configuration file. To add an System.Diagnostics.EventLogTraceListener using an application configuration file, edit the file that corresponds to the name of your application or the app.config file in a vsprvslong project. In this file, insert an element for a System.Diagnostics.EventLogTraceListener type.

The following example adds a System.Diagnostics.EventLogTraceListener object named myListener to the Trace.Listeners collection. The initializeData parameter specifies the name of the event log source that is to be passed to the EventLogTraceListener.#ctor(string) constructor.


    <trace autoflush="false" indentsize="4">
        <add name="myListener"
          initializeData="TraceListenerLog" />

For more information about how to add trace listeners in the application configuration file, see <listeners> element or <listeners> element for <source>.


If the EventLog.Source for the event log that is associated with the System.Diagnostics.EventLogTraceListener instance does not exist, a new event source is created. 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. In Windows Vista, users do not have permission to access the security log; therefore, a System.Security.SecurityException is thrown.

In Windows Vista, User Account Control (UAC) determines the privileges of a user. 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.

The class provides the EventLogTraceListener.EventLog property to get or set the event log that receives the tracing or debugging output, and the EventLogTraceListener.Name property to hold the name of the System.Diagnostics.EventLogTraceListener.

The EventLogTraceListener.Close method closes the event log so it no longer receives tracing or debugging output. The EventLogTraceListener.Write(string) and EventLogTraceListener.WriteLine(string) methods write a message to the event log.


To avoid the possibility of writing large amounts of data to the event log, the System.Diagnostics.EventLogTraceListener does not output the optional trace data specified by the TraceListener.TraceOutputOptions property.


Namespace: System.Diagnostics
Assembly: System (in System.dll)
Assembly Versions: 1.0.3300.0, 1.0.5000.0,,