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
Declare and create the event log. For details, see How to: Create and Remove Custom Event Logs.
Set the Name property of each thread to a unique identifier.
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
Be sure to obtain an exclusive lock on the event log before proceeding with these operations.