Export (0) Print
Expand All

EventLogTraceListener Class

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

System.Object
  System.MarshalByRefObject
    System.Diagnostics.TraceListener
      System.Diagnostics.EventLogTraceListener

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

[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true)]
public sealed class EventLogTraceListener : TraceListener

The EventLogTraceListener type exposes the following members.

  NameDescription
Public methodEventLogTraceListener()Initializes a new instance of the EventLogTraceListener class without a trace listener.
Public methodEventLogTraceListener(EventLog)Initializes a new instance of the EventLogTraceListener class using the specified event log.
Public methodEventLogTraceListener(String)Initializes a new instance of the EventLogTraceListener class using the specified source.
Top

  NameDescription
Public propertyAttributesGets the custom trace listener attributes defined in the application configuration file. (Inherited from TraceListener.)
Public propertyEventLogGets or sets the event log to write to.
Public propertyFilterGets and sets the trace filter for the trace listener. (Inherited from TraceListener.)
Public propertyIndentLevelGets or sets the indent level. (Inherited from TraceListener.)
Public propertyIndentSizeGets or sets the number of spaces in an indent. (Inherited from TraceListener.)
Public propertyIsThreadSafeGets a value indicating whether the trace listener is thread safe. (Inherited from TraceListener.)
Public propertyNameGets or sets the name of this EventLogTraceListener. (Overrides TraceListener.Name.)
Public propertyTraceOutputOptionsGets or sets the trace output options. (Inherited from TraceListener.)
Top

  NameDescription
Public methodCloseCloses the event log so that it no longer receives tracing or debugging output. (Overrides TraceListener.Close().)
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public methodDispose()Releases all resources used by the TraceListener. (Inherited from TraceListener.)
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodFail(String)Emits an error message to the listener you create when you implement the TraceListener class. (Inherited from TraceListener.)
Public methodFail(String, String)Emits an error message and a detailed error message to the listener you create when you implement the TraceListener class. (Inherited from TraceListener.)
Public methodFlushWhen overridden in a derived class, flushes the output buffer. (Inherited from TraceListener.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodGetSupportedAttributesGets the custom attributes supported by the trace listener. (Inherited from TraceListener.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodTraceData(TraceEventCache, String, TraceEventType, Int32, Object)Writes trace information, a data object, and event information to the event log. (Overrides TraceListener.TraceData(TraceEventCache, String, TraceEventType, Int32, Object).)
Public methodTraceData(TraceEventCache, String, TraceEventType, Int32, Object[])Writes trace information, an array of data objects, and event information to the event log. (Overrides TraceListener.TraceData(TraceEventCache, String, TraceEventType, Int32, Object[]).)
Public methodTraceEvent(TraceEventCache, String, TraceEventType, Int32)Writes trace and event information to the listener specific output. (Inherited from TraceListener.)
Public methodTraceEvent(TraceEventCache, String, TraceEventType, Int32, String)Writes trace information, a message, and event information to the event log. (Overrides TraceListener.TraceEvent(TraceEventCache, String, TraceEventType, Int32, String).)
Public methodTraceEvent(TraceEventCache, String, TraceEventType, Int32, String, Object[])Writes trace information, a formatted array of objects, and event information to the event log. (Overrides TraceListener.TraceEvent(TraceEventCache, String, TraceEventType, Int32, String, Object[]).)
Public methodTraceTransferWrites trace information, a message, a related activity identity and event information to the listener specific output. (Inherited from TraceListener.)
Public methodWrite(Object)Writes the value of the object's ToString method to the listener you create when you implement the TraceListener class. (Inherited from TraceListener.)
Public methodWrite(String)Writes a message to the event log for this instance. (Overrides TraceListener.Write(String).)
Public methodWrite(Object, String)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.)
Public methodWrite(String, String)Writes a category name and a message to the listener you create when you implement the TraceListener class. (Inherited from TraceListener.)
Public methodWriteLine(Object)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.)
Public methodWriteLine(String)Writes a message to the event log for this instance. (Overrides TraceListener.WriteLine(String).)
Public methodWriteLine(Object, String)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.)
Public methodWriteLine(String, String)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.)
Top

An 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. 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.Diagnostics.EventLogTraceListener type.

The following example adds a 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(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 <listeners> element or <listeners> element for <source>.

NoteNote

If the Source 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 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.

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.

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

NoteNote

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: Synchronization. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

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 Trace.Listeners collection. Finally, the example sends a line of output to the Listeners 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");
 }
 

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

This type is thread safe.

Show:
© 2014 Microsoft