Provides a simple listener that directs tracing or debugging output to an System.Diagnostics.EventLog.
See Also: EventLogTraceListener Members
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.
<configuration> <system.diagnostics> <trace autoflush="false" indentsize="4"> <listeners> <add name="myListener" type="System.Diagnostics.EventLogTraceListener" initializeData="TraceListenerLog" /> </listeners> </trace> </system.diagnostics> </configuration>
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.