15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Tr a c i n g ❘ 473<br />

To show the call stack <strong>and</strong> the logical call stack with the trace messages, the XmlWriterTraceListener can<br />

be confi gured to the corresponding traceOuputOptions . The MSDN documentation gives details on all the<br />

other options you can confi gure for tracing with this listener.<br />

< sharedListeners ><br />

< add name="xmlListener" type="System.Diagnostics.XmlWriterTraceListener"<br />

traceOutputOptions=" LogicalOperationStack, Callstack "<br />

initializeData="c:/logs/mytrace.xml" ><br />

< /add ><br />

< /sharedListeners ><br />

code snippet TracingDemo/App.config<br />

To see correlation <strong>and</strong> logical call stack in action, in the Main method a new activity ID is assigned to the<br />

CorrelationManager by setting the ActivityID property. Events of type TraceEventType.Start <strong>and</strong><br />

TraceEventType.Stop are done at the beginning <strong>and</strong> end of the Main method. Also, a logical operation named<br />

“ Main ” is started <strong>and</strong> stopped with the StartLogicalOperation() <strong>and</strong> StopLogicalOperation() methods.<br />

static void Main()<br />

{<br />

// start a new activity<br />

if (Trace.CorrelationManager.ActivityId == Guid.Empty)<br />

{<br />

Guid newGuid = Guid.NewGuid();<br />

Trace.CorrelationManager.ActivityId = newGuid;<br />

}<br />

trace.TraceEvent(TraceEventType.Start, 0, "Main started");<br />

// start a logical operation<br />

Trace.CorrelationManager.StartLogicalOperation("Main");<br />

}<br />

TraceSourceDemo1();<br />

StartActivityA();<br />

Trace.CorrelationManager.StopLogicalOperation();<br />

Thread.Sleep(3000);<br />

trace.TraceEvent(TraceEventType.Stop, 0, "Main stopped");<br />

code snippet TracingDemo/Program.cs<br />

The method StartActivityA() that is called from within the Main() method creates a new activity<br />

by setting the ActivityId of the CorrelationManager to a new GUID. Before the activity stops, the<br />

ActivityId of the CorrelationManager is reset to the previous value. This method invokes the Foo()<br />

method <strong>and</strong> creates a new task with the Task.Factory.StartNew() method. This task is created so that<br />

you can see how different threads are displayed in a trace viewer.<br />

Tasks are explained in Chapter 20, “Threads, Tasks, <strong>and</strong> Synchronization.”<br />

private static void StartActivityA()<br />

{<br />

Guid oldGuid = Trace.CorrelationManager.ActivityId;<br />

Guid newActivityId = Guid.NewGuid();<br />

Trace.CorrelationManager.ActivityId = newActivityId;<br />

Trace.CorrelationManager.StartLogicalOperation("StartActivityA");<br />

trace.TraceEvent(TraceEventType.Verbose, 0,<br />

"starting Foo in StartNewActivity");<br />

Foo();<br />

trace.TraceEvent(TraceEventType.Verbose, 0,<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!