CorrelationManager Klasa

Definicja

Koreluje ślady, które są częścią transakcji logicznej.

public ref class CorrelationManager
public class CorrelationManager
type CorrelationManager = class
Public Class CorrelationManager
Dziedziczenie
CorrelationManager

Przykłady

Poniższy przykład kodu przedstawia użycie CorrelationManager klasy przez zidentyfikowanie operacji logicznej skojarzonej ze śledznym zdarzeniem. Dwa operacje logiczne są uruchamiane, jeden w wątku głównym i drugi w wątku roboczym. Zdarzenie błędu jest rejestrowane w obu operacjach logicznych.

#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

Uwagi

Ślady generowane na podstawie pojedynczej operacji logicznej można otagować za pomocą tożsamości unikatowej operacji, aby odróżnić je od śladów od innej operacji logicznej. Na przykład może być przydatne grupowanie skorelowanych śladów przez żądanie ASP.NET. Klasa CorrelationManager udostępnia metody używane do przechowywania tożsamości operacji logicznej w kontekście powiązanym z wątkiem i automatycznego tagowania każdego zdarzenia śledzenia wygenerowanego przez wątek przy użyciu przechowywanej tożsamości.

Operacje logiczne można również zagnieżdżać. Właściwość LogicalOperationStack uwidacznia stos zagnieżdżonych tożsamości operacji logicznych. Każde wywołanie metody StartLogicalOperation wypycha nową tożsamość operacji logicznej do stosu. Każde wywołanie StopLogicalOperation metody uruchamia tożsamość operacji logicznej poza stosem.

Uwaga

Tożsamości operacji logicznych to obiekty, które umożliwiają użycie typu dla tożsamości operacji logicznej.

Właściwości

ActivityId

Pobiera lub ustawia tożsamość dla działania globalnego.

LogicalOperationStack

Pobiera stos operacji logicznych z kontekstu wywołania.

Metody

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
StartLogicalOperation()

Uruchamia operację logiczną w wątku.

StartLogicalOperation(Object)

Uruchamia operację logiczną z określoną tożsamością w wątku.

StopLogicalOperation()

Zatrzymuje bieżącą operację logiczną.

ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Dotyczy