CorrelationManager 類別

定義

讓屬於邏輯異動一部分的追蹤產生關聯。

public ref class CorrelationManager
public class CorrelationManager
type CorrelationManager = class
Public Class CorrelationManager
繼承
CorrelationManager

範例

下列程式碼範例示範如何使用 CorrelationManager 類別,方法是識別與追蹤事件相關聯的邏輯作業。 啟動兩個邏輯作業,一個在主執行緒中,另一個在背景工作執行緒中。 錯誤事件會記錄在這兩個邏輯作業中。

#using <System.dll>

using namespace System;
using namespace System::Collections::Generic;
using namespace System::Text;
using namespace System::Diagnostics;
using namespace System::Threading;

void ThreadProc()
{
    TraceSource^ ts = gcnew TraceSource("MyApp");
    int i = ts->Listeners->Add(gcnew ConsoleTraceListener());
    ts->Listeners[i]->TraceOutputOptions = TraceOptions::LogicalOperationStack;
    ts->Switch = gcnew SourceSwitch("MyAPP", "Verbose");
    // Add another logical operation.
    Trace::CorrelationManager->StartLogicalOperation("WorkerThread");

    ts->TraceEvent(TraceEventType::Error, 1, "Trace an error event.");
    Trace::CorrelationManager->StopLogicalOperation();
}

void main()
{
    TraceSource^ ts = gcnew TraceSource("MyApp");
    int i = ts->Listeners->Add(gcnew ConsoleTraceListener());
    ts->Listeners[i]->TraceOutputOptions = TraceOptions::LogicalOperationStack;
    ts->Switch = gcnew SourceSwitch("MyAPP", "Verbose");
    // Start the logical operation on the Main thread.
    Trace::CorrelationManager->StartLogicalOperation("MainThread");

    ts->TraceEvent(TraceEventType::Error, 1, "Trace an error event.");
    Thread^ t = gcnew Thread(gcnew ThreadStart(ThreadProc));
//   Start the worker thread.
    t->Start();
//  Give the worker thread a chance to execute.
    Thread::Sleep(1000);
    Trace::CorrelationManager->StopLogicalOperation();}



// This sample generates the following output:
//MyApp Error: 1 : Trace an error event.
//    LogicalOperationStack=MainThread
//MyApp Error: 1 : Trace an error event.
//    LogicalOperationStack=WorkerThread, MainThread
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;

namespace CorrlationManager
{
    class Program
    {
        //private static TraceSource ts;
        static void Main(string[] args)
        {
            TraceSource ts = new TraceSource("MyApp");
            int i = ts.Listeners.Add(new ConsoleTraceListener());
            ts.Listeners[i].TraceOutputOptions = TraceOptions.LogicalOperationStack;
            ts.Switch = new SourceSwitch("MyAPP", "Verbose");
            // Start the logical operation on the Main thread.
            Trace.CorrelationManager.StartLogicalOperation("MainThread");
            ts.TraceEvent(TraceEventType.Error, 1, "Trace an error event.");
            Thread t = new Thread(new ThreadStart(ThreadProc));
            // Start the worker thread.
            t.Start();
            // Give the worker thread a chance to execute.
            Thread.Sleep(1000);
            Trace.CorrelationManager.StopLogicalOperation();
        }
        public static void ThreadProc()
        {
            TraceSource ts = new TraceSource("MyApp");
            int i = ts.Listeners.Add(new ConsoleTraceListener());
            ts.Listeners[i].TraceOutputOptions = TraceOptions.LogicalOperationStack;
            ts.Switch = new SourceSwitch("MyAPP", "Verbose");
            // Add another logical operation.
            Trace.CorrelationManager.StartLogicalOperation("WorkerThread");
            ts.TraceEvent(TraceEventType.Error, 1, "Trace an error event.");
            Trace.CorrelationManager.StopLogicalOperation();
        }
    }
}
// This sample generates the following output:
//MyApp Error: 1 : Trace an error event.
//    LogicalOperationStack=MainThread
//MyApp Error: 1 : Trace an error event.
//    LogicalOperationStack=WorkerThread, MainThread
Imports System.Collections.Generic
Imports System.Text
Imports System.Diagnostics
Imports System.Threading



Class Program
    
    'private static TraceSource ts;
    Shared Sub Main(ByVal args() As String) 
        Dim ts As New TraceSource("MyApp")
        Dim i As Integer = ts.Listeners.Add(New ConsoleTraceListener())
        ts.Listeners(i).TraceOutputOptions = TraceOptions.LogicalOperationStack
        ts.Switch = New SourceSwitch("MyAPP", "Verbose")
        ' Start the logical operation on the Main thread.
        Trace.CorrelationManager.StartLogicalOperation("MainThread")
        ts.TraceEvent(TraceEventType.Error, 1, "Trace an error event.")
        Dim t As New Thread(New ThreadStart(AddressOf ThreadProc))
        ' Start the worker thread.
        t.Start()
        ' Give the worker thread a chance to execute.
        Thread.Sleep(1000)
        Trace.CorrelationManager.StopLogicalOperation()
    
    End Sub
    
    Public Shared Sub ThreadProc() 
        Dim ts As New TraceSource("MyApp")
        Dim i As Integer = ts.Listeners.Add(New ConsoleTraceListener())
        ts.Listeners(i).TraceOutputOptions = TraceOptions.LogicalOperationStack
        ts.Switch = New SourceSwitch("MyAPP", "Verbose")
        ' Add another logical operation.
        Trace.CorrelationManager.StartLogicalOperation("WorkerThread")
        ts.TraceEvent(TraceEventType.Error, 1, "Trace an error event.")
        Trace.CorrelationManager.StopLogicalOperation()
    
    End Sub
End Class
' This sample generates the following output:
'MyApp Error: 1 : Trace an error event.
'    LogicalOperationStack=MainThread
'MyApp Error: 1 : Trace an error event.
'    LogicalOperationStack=WorkerThread, MainThread

備註

從單一邏輯作業產生的追蹤可以使用作業唯一的身分識別來標記,以區別它們與不同邏輯作業的追蹤。 例如,透過 ASP.NET 要求將相互關聯的追蹤分組可能很有用。 類別 CorrelationManager 提供用來將邏輯作業身分識別儲存線上程系結內容中的方法,並使用預存身分識別自動標記執行緒所產生的每個追蹤事件。

邏輯作業也可以巢狀化。 屬性 LogicalOperationStack 會公開巢狀邏輯作業識別的堆疊。 每次呼叫 方法時 StartLogicalOperation ,都會將新的邏輯作業身分識別推送至堆疊。 每次呼叫 方法時 StopLogicalOperation ,都會從堆疊中快顯邏輯作業身分識別。

注意

邏輯作業身分識別是物件,允許針對邏輯作業身分識別使用型別。

屬性

ActivityId

取得或設定全域活動的識別。

LogicalOperationStack

從呼叫內容取得邏輯作業堆疊。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
StartLogicalOperation()

啟動執行緒上的邏輯作業。

StartLogicalOperation(Object)

用執行緒上指定的識別啟動邏輯作業。

StopLogicalOperation()

停止目前的邏輯作業。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於