CorrelationManager 管理逻辑

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
namespace DiagnosticStuff
{
    class Program
    {
        static void Main(string[] args)
        {
            Program temp = new Program();
            temp.Test();
            Console.ReadKey();
        }
        public void Test()
        {
            TextWriterTraceListener listener = new TextWriterTraceListener(@"c:\tracefile.txt");
            listener.TraceOutputOptions |= TraceOptions.LogicalOperationStack;
            Trace.Listeners.Add(listener);
            Trace.CorrelationManager.StartLogicalOperation("Main");
            Trace.TraceInformation("Hello there");
            Thread ts1 = new Thread(new ThreadStart(EatContext));
            Thread ts2 = new Thread(new ThreadStart(DrinkContext));
            ts1.Start();
            ts2.Start();
            ts1.Join();
            ts2.Join();
            Trace.CorrelationManager.StopLogicalOperation();
            listener.Close();

        }
        public void EatContext()
        {
            Trace.TraceInformation("Swallow 0x{0}",Thread.CurrentThread.GetHashCode());
            Trace.TraceInformation("EatContextend");
            Thread.Sleep(20);
        }
        public void DrinkContext()
        {
            Trace.TraceInformation("Pickup Drink 0x{0}",Thread.CurrentThread.GetHashCode());
            Trace.TraceInformation("DrinkContextend");
            Thread.Sleep(20);
        }
    }
}

posted on 2012-12-02 16:32  R.Ray  阅读(156)  评论(0)    收藏  举报

导航