방법: 다중 스레드 구성 요소에 대한 이벤트 기록

다중 스레드 구성 요소에서 발생하는 이벤트를 기록할 때 특별히 고려할 사항이 있습니다.이벤트를 발생시킨 스레드를 식별하는 방법을 제공해야 하며,이벤트를 기록할 때 스레드들 간에 서로 방해하지 않도록 해야 합니다.자세한 내용은 이벤트 로그와 다중 스레드 구성 요소를 참조하십시오.

다중 스레드 응용 프로그램에서 이벤트 로그를 사용하려면

  1. 이벤트 로그를 선언하고 만듭니다.자세한 내용은 How to: Create and Remove Custom Event Logs를 참조하십시오.

  2. 각 스레드의 Name 속성을 고유 식별자로 설정합니다.

    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. 이벤트 로그를 위한 새 Source를 만들고 스레드에 해당하는 고유한 문자열 값을 Name에 설정합니다.Source 만들기에 대한 자세한 내용은 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. 이벤트 로그의 Source 속성을 스레드 이름으로 설정하고 WriteEntry 메서드를 호출합니다.

    [!참고]

    이러한 작업을 진행하기 전에 이벤트 로그에 단독 잠금을 설정해야 합니다.

    ' 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");
    }
    

참고 항목

작업

방법: 다중 스레드 실행 조정

연습: Visual Basic으로 간단한 다중 스레드 구성 요소 만들기

연습: Visual C#으로 간단한 다중 스레드 구성 요소 만들기

참조

SyncLock 문

기타 리소스

How to: Create and Remove Custom Event Logs

How to: Write Entries to Event Logs

구성 요소에서 다중 스레딩