CorrelationManager CorrelationManager CorrelationManager CorrelationManager Class

Definizione

Mette in correlazione le tracce che fanno parte di una transazione logica.Correlates traces that are part of a logical transaction.

public ref class CorrelationManager
public class CorrelationManager
type CorrelationManager = class
Public Class CorrelationManager
Ereditarietà
CorrelationManagerCorrelationManagerCorrelationManagerCorrelationManager

Esempi

Esempio di codice seguente viene illustrato l'utilizzo del CorrelationManager classe identificando l'operazione logica associata a un evento tracciato.The following code example demonstrates the use of the CorrelationManager class by identifying the logical operation associated with a traced event. Vengono avviate due operazioni logiche, uno nel thread principale e l'altra in un thread di lavoro.Two logical operations are started, one in the main thread and the other in a worker thread. Un evento di errore viene registrato in entrambe le operazioni logiche.An error event is logged in both logical operations.

#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
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 'Main
    
    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 'ThreadProc
End Class 'Program
' 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

Commenti

Le tracce generate da una singola operazione logica possono essere contrassegnate con un'identità univoca operazione, per distinguerle da quelle prodotte da un'operazione logica diversa.Traces generated from a single logical operation can be tagged with an operation-unique identity, in order to distinguish them from traces from a different logical operation. Ad esempio, può essere utile raggruppare le tracce correlate dalla richiesta ASP.NET.For example, it may be useful to group correlated traces by ASP.NET request. Il CorrelationManager classe fornisce metodi usati per archiviare un'identità operazione logica in un contesto di thread associati e contrassegnare automaticamente ogni evento di traccia generata dal thread con l'identità memorizzata.The CorrelationManager class provides methods used to store a logical operation identity in a thread-bound context and automatically tag each trace event generated by the thread with the stored identity.

Possono anche essere annidate operazioni logiche.Logical operations can also be nested. Il LogicalOperationStack proprietà espone lo stack di identità operazione logica annidata.The LogicalOperationStack property exposes the stack of nested logical operation identities. Ogni chiamata al StartLogicalOperation metodo inserisce una nuova identità operazione logica nello stack.Each call to the StartLogicalOperation method pushes a new logical operation identity onto the stack. Ogni chiamata al StopLogicalOperation metodo estrae un'identità di operazioni logiche dallo stack.Each call to the StopLogicalOperation method pops a logical operation identity off the stack.

Nota

Identità di operazioni logiche sono oggetti, consentendo l'uso di un tipo per un'identità di operazioni logiche.Logical operation identities are objects, allowing the use of a type for a logical operation identity.

Proprietà

ActivityId ActivityId ActivityId ActivityId

Ottiene o imposta l'identità per un'attività globale.Gets or sets the identity for a global activity.

LogicalOperationStack LogicalOperationStack LogicalOperationStack LogicalOperationStack

Ottiene lo stack di operazioni logiche dal contesto di chiamata.Gets the logical operation stack from the call context.

Metodi

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Funge da funzione hash predefinita.Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Ottiene l'oggetto Type dell'istanza corrente.Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.Creates a shallow copy of the current Object.

(Inherited from Object)
StartLogicalOperation() StartLogicalOperation() StartLogicalOperation() StartLogicalOperation()

Avvia un'operazione logica su un thread.Starts a logical operation on a thread.

StartLogicalOperation(Object) StartLogicalOperation(Object) StartLogicalOperation(Object) StartLogicalOperation(Object)

Avvia un'operazione logica con l'identità specificata su un thread.Starts a logical operation with the specified identity on a thread.

StopLogicalOperation() StopLogicalOperation() StopLogicalOperation() StopLogicalOperation()

Interrompe l'operazione logica corrente.Stops the current logical operation.

ToString() ToString() ToString() ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.Returns a string that represents the current object.

(Inherited from Object)

Si applica a