Home All Groups Group Topic Archive Search About

CorrelationManager and Trace

Author
20 Dec 2005 6:22 AM
Raghu
I am trying to figure out how to use CorrelationManager effectively in tracing.

The code (shown at the end) produces following output:

******
MyAsm.exe Information: 0 : Hello there!
    LogicalOperationStack=Main
MyAsm.exe Information: 0 : Pickup Pizza 0xC
    LogicalOperationStack=Drink Context, Pizza Context, Main
MyAsm.exe Information: 0 : Pickup Drink 0xD
    LogicalOperationStack=Drink Context, Pizza Context, Main
MyAsm.exe Information: 0 : Open Mouth 0xC
    LogicalOperationStack=Drink Context, Pizza Context, Main
MyAsm.exe Information: 0 : Open Mouth 0xD
    LogicalOperationStack=Drink Context, Pizza Context, Main
MyAsm.exe Information: 0 : Chew 0xC
    LogicalOperationStack=Drink Context, Pizza Context, Main
MyAsm.exe Information: 0 : Swallow 0xD
    LogicalOperationStack=Drink Context, Pizza Context, Main
******

Other than the first line, all other lines consists of 3 levels of stack:
Drink Context, Pizza Context and Main. However I was only expecting two sets
of levels: Drink Context and Main; Pizza Context  and Main only. Am I doing
some thing wrong?

Code follows **********

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;

class myns
{
    class DiagnosticStuff
    {
        public void Test()
        {
            //Configure console trace listener
            //ConsoleTraceListener listener = new ConsoleTraceListener();
            TextWriterTraceListener listener = new
TextWriterTraceListener(@"C:\tracefile.txt");

            listener.TraceOutputOptions |= TraceOptions.LogicalOperationStack;
            Trace.Listeners.Add(listener);

            Trace.CorrelationManager.StartLogicalOperation("Main");

            Trace.TraceInformation("Hello there!");

            //Create 2 threads, one with each context
            Thread[] ts = new Thread[2];

            ts[0] = new Thread(new ThreadStart(PizzaContext));
            ts[1] = new Thread(new ThreadStart(DrinkContext));

            ts[0].Start();
            ts[1].Start();

            ts[0].Join();
            ts[1].Join();

            //Trace.Listeners.Remove(listener);
            Trace.CorrelationManager.StopLogicalOperation();

            listener.Close();
        }

        private void PizzaContext()
        {
            Trace.CorrelationManager.StartLogicalOperation("Pizza Context");

            PickupPizza();
            OpenMouth();
            Chew();

            Trace.CorrelationManager.StopLogicalOperation();
        }

        private void DrinkContext()
        {
            Trace.CorrelationManager.StartLogicalOperation("Drink Context");

            PickupDrink();
            OpenMouth();
            Swallow();

            Trace.CorrelationManager.StopLogicalOperation();
        }

        private void PickupPizza()
        {
            Trace.TraceInformation("Pickup Pizza 0x{0:X}",
Thread.CurrentThread.GetHashCode());
            Thread.Sleep(20);
        }

        private void OpenMouth()
        {
            Trace.TraceInformation("Open Mouth 0x{0:X}",
Thread.CurrentThread.GetHashCode());
            Thread.Sleep(20);
        }

        private void Chew()
        {
            Trace.TraceInformation("Chew 0x{0:X}",
Thread.CurrentThread.GetHashCode());
            Thread.Sleep(20);
        }

        private void PickupDrink()
        {
            Trace.TraceInformation("Pickup Drink 0x{0:X}",
Thread.CurrentThread.GetHashCode());
            Thread.Sleep(20);
        }

        private void Swallow()
        {
            Trace.TraceInformation("Swallow 0x{0:X}",
Thread.CurrentThread.GetHashCode());
            Thread.Sleep(20);
        }
    }
}

Raghu/..

AddThis Social Bookmark Button