CorrelationManager Class
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Correlates traces that are part of a logical transaction.
public ref class CorrelationManager
public class CorrelationManager
type CorrelationManager = class
Public Class CorrelationManager
- Inheritance
-
CorrelationManager
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
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
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.
הערה
Logical operation identities are objects, allowing the use of a type for a logical operation identity.
Activity |
Gets or sets the identity for a global activity. |
Logical |
Gets the logical operation stack from the call context. |
Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
Get |
Serves as the default hash function. (Inherited from Object) |
Get |
Gets the Type of the current instance. (Inherited from Object) |
Memberwise |
Creates a shallow copy of the current Object. (Inherited from Object) |
Start |
Starts a logical operation on a thread. |
Start |
Starts a logical operation with the specified identity on a thread. |
Stop |
Stops the current logical operation. |
To |
Returns a string that represents the current object. (Inherited from Object) |
מוצר | גירסאות |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
משוב של .NET
.NET הוא פרויקט קוד פתוח. בחר קישור כדי לספק משוב: