Compartir a través de


How to: Log Events for Multithreaded Components

When logging events from multithreaded components, special considerations come into play. You must supply a means of identifying the thread from which the event came. You must also ensure that the threads do not interfere with one another when logging events. For details, see Event Logs and Multithreaded Components.

To use event logs in multithreaded applications

  1. Declare and create the event log. For details, see How to: Create and Remove Custom Event Logs.

  2. Set the Name property of each thread to a unique identifier.

    Dim X as Integer
    X = 0
    Dim MyThread as New Threading.Thread(AddressOf MyMethod)
    MyThread.Name = "Thread number " & X.ToString
    ' Increments X so no other threads will share the same name.
    X += 1
    
    int X;
    X = 0;
    Thread MyThread = new Thread(new 
       ThreadStart(MyMethod));
    MyThread.Name = "Thread Number " + X.ToString();
    // Increments X so no other threads will share the same name.
    X += 1;
    
  3. Create a new Source for the event log, and set the Name to a unique string value that corresponds to the thread. For details on creating a Source, see How to: Add Your Application as a Source of Event Log Entries.

    Imports System.Threading
    ' Checks to see if there already is a source with the name of the 
    ' thread.
    If Not EventLog.SourceExists(Thread.CurrentThread.Name.ToString, _
       "myApplication") Then
       ' Creates a source with the name of the thread
       EventLog.CreateEventSource(Thread.CurrentThread.Name.ToString, _
          "MyApplication")
    End If
    
    // These lines go at the top of the code
    using System.Threading;
    using System.Diagnostics;
    // Checks to see if there already is a source with the name of the 
    // thread.
    if (!EventLog.SourceExists(CurrentThread.Name.ToString()))
       // Creates a source with the name of the thread.
       EventLog.CreateEventSource(CurrentThread.Name.ToString(), 
          "MyApplication");
    
  4. Set the Source property of the event log to the name of the thread and call the WriteEntry method.

    Note

    Be sure to obtain an exclusive lock on the event log before proceeding with these operations.

    ' Use the SyncLock keyword to obtain an exclusive lock on an object.
    SyncLock MyEventLog
      MyEventLog.Source = Thread.CurrentThread.Name.ToString
      EventLog.WriteEntry(Thread.CurrentThread.Name.ToString, _
         "Error in Widget 42")
    End SyncLock
    
    // Use the lock keyword to obtain an exclusive lock on an object
    lock(MyEventLog)
    {
      MyEventLog.Source = Thread.CurrentThread.Name.ToString();
      EventLog.WriteEntry(Thread.CurrentThread.Name.ToString(),
         "Error in Widget 42");
    }
    

See Also

Tasks

How to: Coordinate Multiple Threads of Execution

Walkthrough: Authoring a Simple Multithreaded Component with Visual Basic

Walkthrough: Authoring a Simple Multithreaded Component with Visual C#

Reference

SyncLock Statement

Other Resources

How to: Create and Remove Custom Event Logs

How to: Write Entries to Event Logs

Multithreading in Components