Implementation of the Correlation Manager class

The following code sample shows how to use the CorrelationManager class programmatically in Visual Basic and C#. In this code sample, you are creating two methods, EatContext and DrinkContext. These methods are executed concurrently on different threads. For the listener to know what information is received from each method, you call the Trace.CorrelationManager.StartLogicalOperation method. Now, you can distinguish trace information based on method and thread.

Visual Basic

Imports System.Threading

Class DiagnosticStuff

Public Sub Test()

'Configure console trace listener

Dim listener As TextWriterTraceListener = New

TextWri terTraceListener("C:\tracefile.txt")

li stener.TraceOutputOptions = TraceOptions.Logi calOperationStack

Trace.Li steners.Add(listener)

'CorrelationManager: It makes sure that tracing within the

'threads created in "Main" is ONE logical transaction.

Trace.CorrelationManager.StartLogicalOpe rati on("Main")

Trace.TraceInformation("Hello there!")

'Create 2 threads, one with each context

Dim ts1 As Thread = New Thread(New ThreadStart(AddressOf EatContext))

Dim ts2 As Thread = New Thread(New ThreadStart(AddressOf DrinkContext))

ts1. Start()

ts2. Start()

'Makes sure Test() is blocked till the execution of ts1 is

completed

ts1.Join()

'Makes sure Test() is blocked till the execution of ts2 is

completed

ts2.Join()

Trace.CorrelationManager.StopLogicalOperation()

listener.Close()

End Sub

Shared Sub Main()

Dim temp As DiagnosticStuff = New DiagnosticStuff

temp.Test()

End Sub

Private Sub EatContext()

Trace.TraceInformation("Swallow 0x{0:X}" _

, Thread.CurrentThread.GetHashCode())

Thread.Sleep(20)

Trace.CorrelationManager.StopLogicalOperation()

End Sub

Private Sub DrinkContext()

Trace.TraceInformation("Pickup Drink 0x{0:X}" _

, Thread.CurrentThread.GetHashCode())

Thread.Sleep(20)

Trace.CorrelationManager.StopLogicalOperation()

End Sub

End Class

new DiagnosticStuff();

using System; using System.Threading; using System.Diagnostics;

class DiagnosticStuff {

static void Main() {

DiagnosticStuff temp temp.Test(); Console.ReadLine();

public void Test() {

//Configure console trace listener TextWriterTraceListener listener = new TextWri terTraceListener(@"C:\tracefile.txt");

li stener.TraceOutputOptions |= TraceOpti ons.LogicalOperationStack; Trace.Listeners.Add(listener);

//CorrelationManager: It makes sure that tracing within the //threads created in "Main" is ONE logical transaction. Trace.CorrelationManager.StartLogicalOpe rati on("Main"); Trace.TraceInformation("Hello there!"); //Create 2 threads, one with each context Thread[] ts = new Thread[2];

//Makes sure Test() is blocked till the execution of ts[0] is completed ts[0] = new Thread(new ThreadStart(EatContext));

//Makes sure Test() is blocked till the execution of ts[1] is completed ts[1] = new Thread(new ThreadStart(DrinkContext));

Trace.CorrelationManager.StopLogicalOperation(); listener.Close() ;

private void EatContext() {

Trace.CorrelationManager.StartLogicalOperati on("Eat Context"); Trace.TraceInformation("Pickup EatContext 0x{0:X}", Thread.CurrentThread.GetHashCode()); Thread.Sleep(20) ;

Trace.CorrelationManager.StopLogicalOperation();

private void DrinkContext() {

Trace.CorrelationManager.StartLogicalOperation("Drink Context"); Trace.TraceInformation("Pickup Drink 0x{0:X}", Thread.CurrentThread.GetHashCode()); Thread.Sleep(20) ;

Trace.CorrelationManager.StopLogicalOperation();

Discussion: Identifying the Options Available for Application Tracing

What is application tracing? What is the Trace class? What is a trace source? What is the

CorrelationManager class? What is a trace listener?

The .NET Framework provides classes to monitor an application's execution. In this discussion, you will identify the options available to trace applications programmatically.

Answer the questions on the slide and discuss the answers in the class.

Was this article helpful?

0 0

Post a comment