EventLogTraceListener Class
 

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

Namespace:   System.Diagnostics
Assembly:  System (in System.dll)

Inheritance Hierarchy
SystemObject
   SystemMarshalByRefObject
     System.DiagnosticsTraceListener
      System.DiagnosticsEventLogTraceListener

Syntax
[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true)]
public sealed class EventLogTraceListener : TraceListener
[HostProtectionAttribute(SecurityAction::LinkDemand, Synchronization = true)]
public ref class EventLogTraceListener sealed : TraceListener
[<Sealed>]
[<HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true)>]
type EventLogTraceListener = 
    class
        inherit TraceListener
    end
<HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization := True)>
Public NotInheritable Class EventLogTraceListener
	Inherits TraceListener
Constructors
NameDescription
System_CAPS_pubmethod EventLogTraceListener

Initializes a new instance of the EventLogTraceListener class without a trace listener.

System_CAPS_pubmethod EventLogTraceListener

Initializes a new instance of the EventLogTraceListener class using the specified event log.

System_CAPS_pubmethod EventLogTraceListener

Initializes a new instance of the EventLogTraceListener class using the specified source.

Properties
NameDescription
System_CAPS_pubproperty

Gets the custom trace listener attributes defined in the application configuration file.(Inherited from TraceListener.)

System_CAPS_pubproperty

Gets or sets the event log to write to.

System_CAPS_pubproperty

Gets and sets the trace filter for the trace listener.(Inherited from TraceListener.)

System_CAPS_pubproperty

Gets or sets the indent level.(Inherited from TraceListener.)

System_CAPS_pubproperty

Gets or sets the number of spaces in an indent.(Inherited from TraceListener.)

System_CAPS_pubproperty

Gets a value indicating whether the trace listener is thread safe. (Inherited from TraceListener.)

System_CAPS_pubproperty

Gets or sets the name of this EventLogTraceListener.(Overrides .)

System_CAPS_pubproperty

Gets or sets the trace output options.(Inherited from TraceListener.)

Methods
NameDescription
System_CAPS_pubmethod Close

Closes the event log so that it no longer receives tracing or debugging output.(Overrides TraceListenerClose.)

System_CAPS_pubmethod CreateObjRef

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethod Dispose

Releases all resources used by the TraceListener.(Inherited from TraceListener.)

System_CAPS_pubmethod Equals

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethod Fail

Emits an error message to the listener you create when you implement the TraceListener class.(Inherited from TraceListener.)

System_CAPS_pubmethod Fail

Emits an error message and a detailed error message to the listener you create when you implement the TraceListener class.(Inherited from TraceListener.)

System_CAPS_pubmethod Flush

When overridden in a derived class, flushes the output buffer.(Inherited from TraceListener.)

System_CAPS_pubmethod GetHashCode

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethod GetLifetimeService

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethod GetSupportedAttributes

Gets the custom attributes supported by the trace listener.(Inherited from TraceListener.)

System_CAPS_pubmethod GetType

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethod InitializeLifetimeService

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethod ToString

Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubmethod TraceData

Writes trace information, a data object, and event information to the event log.(Overrides TraceListenerTraceData.)

System_CAPS_pubmethod TraceData

Writes trace information, an array of data objects, and event information to the event log.(Overrides TraceListenerTraceData.)

System_CAPS_pubmethod TraceEvent

Writes trace and event information to the listener specific output.(Inherited from TraceListener.)

System_CAPS_pubmethod TraceEvent

Writes trace information, a message, and event information to the event log.(Overrides TraceListenerTraceEvent.)

System_CAPS_pubmethod TraceEvent

Writes trace information, a formatted array of objects, and event information to the event log.(Overrides TraceListenerTraceEvent.)

System_CAPS_pubmethod TraceTransfer

Writes trace information, a message, a related activity identity and event information to the listener specific output.(Inherited from TraceListener.)

System_CAPS_pubmethod Write

Writes the value of the object's ToString method to the listener you create when you implement the TraceListener class.(Inherited from TraceListener.)

System_CAPS_pubmethod Write

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.)

System_CAPS_pubmethod Write

Writes a message to the event log for this instance.(Overrides TraceListenerWrite.)

System_CAPS_pubmethod Write

Writes a category name and a message to the listener you create when you implement the TraceListener class.(Inherited from TraceListener.)

System_CAPS_pubmethod WriteLine

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.)

System_CAPS_pubmethod WriteLine

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.)

System_CAPS_pubmethod WriteLine

Writes a message to the event log for this instance.(Overrides TraceListenerWriteLine.)

System_CAPS_pubmethod WriteLine

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.)

Remarks

An instance of this EventLogTraceListener can be added to the , , or 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. 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 2005 project. In this file, insert an element for a System.DiagnosticsEventLogTraceListener type.

The following example adds a EventLogTraceListener object named myListener to the collection. The initializeData parameter specifies the name of the event log source that is to be passed to the EventLogTraceListener 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 .

System_CAPS_noteNote

If the for the event log that is associated with the 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 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 property to get or set the event log that receives the tracing or debugging output, and the property to hold the name of the EventLogTraceListener.

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

System_CAPS_noteNote

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 property.

Examples

The following example creates a trace listener that sends output to an event log. First, the code creates a new EventLogTraceListener that uses the source myEventLogSource. Next, myTraceListener is added to the collection. Finally, the example sends a line of output to the object.

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 'Main
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
}
Version Information
.NET Framework
Available since 1.1
Thread Safety

This type is thread safe.

Return to top