CorrelationManager Class
Correlates traces that are part of a logical transaction.
Assembly: System (in System.dll)
The CorrelationManager type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | ActivityId | Gets or sets the identity for a global activity. |
![]() | LogicalOperationStack | Gets the logical operation stack from the call context. |
| Name | Description | |
|---|---|---|
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | StartLogicalOperation() | Starts a logical operation on a thread. |
![]() | StartLogicalOperation(Object) | Starts a logical operation with the specified identity on a thread. |
![]() | StopLogicalOperation | Stops the current logical operation. |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
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. For example, it may be useful to group correlated traces by ASP.NET request. 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.
Logical operations can also be nested. The LogicalOperationStack property exposes the stack of nested logical operation identities. Each call to the StartLogicalOperation method pushes a new logical operation identity onto the stack. Each call to the StopLogicalOperation method pops a logical operation identity off the stack.
Note |
|---|
Logical operation identities are objects, allowing the use of a type for a logical operation identity. |
The following code example demonstrates the use of the CorrelationManager class by identifying the logical operation associated with a traced event. Two logical operations are started, one in the main thread and the other in a worker thread. 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
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
