Export (0) Print
Expand All

EventLogTraceListener Class

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

For a list of all members of this type, see EventLogTraceListener Members.

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

[Visual Basic]
NotInheritable Public Class EventLogTraceListener
   Inherits TraceListener
[C#]
public sealed class EventLogTraceListener : TraceListener
[C++]
public __gc __sealed class EventLogTraceListener : public
   TraceListener
[JScript]
public class EventLogTraceListener extends TraceListener

Thread Safety

This type is safe for multithreaded operations.

Remarks

An instance of this EventLogTraceListener can be added to the Debug.Listeners or Trace.Listeners collections to redirect output from tracing or debugging to an EventLog.

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.

Example

[Visual Basic, C#, C++] The following example creates a trace listener that outputs to an event log. First, the code creates an event log called myEventLog. Then, a new EventLogTraceListener is created that uses myEventLog. Next, myTraceListener is added to the Trace.Listeners collection. Finally, the example outputs a line to the Listeners object.

[Visual Basic] 
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

[C#] 
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");
 }
 

[C++] 
int main() {

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

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

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Diagnostics

Platforms: Windows NT Server 4.0, Windows NT Workstation 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

EventLogTraceListener Members | System.Diagnostics Namespace | TraceListener | DefaultTraceListener | TextWriterTraceListener | EventLog | Stream | TextWriter | Debug | Trace

Show:
© 2014 Microsoft