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 in g ❘ 467<br />

TraCing<br />

With tracing, you can see messages from the running application. To get information about a running<br />

application, you can start the application in the debugger. During debugging, you can walk through<br />

the application step by step <strong>and</strong> set breakpoints at specifi c lines <strong>and</strong> when you reach specifi c conditions. The<br />

problem with debugging is that a program with release code can behave differently from a program with<br />

debug code. For example, while the program is stopping at a breakpoint, other threads of the application<br />

are suspended as well. Also, with a release build, the compiler - generated output is optimized <strong>and</strong>, thus,<br />

different effects can occur. There is a need to have information from a release build as well. Trace messages<br />

are written with both debug <strong>and</strong> release code.<br />

A scenario showing how tracing helps is described here. After an application is deployed, it runs on<br />

one system without problems, while on another system intermediate problems occur. When you turn on<br />

verbose tracing, the system with the problems gives you detailed information about what ’ s happening inside<br />

the application. The system that is running without problems has tracing confi gured just for error messages<br />

redirected to the Windows event log system. Critical errors are seen by the system administrator. The<br />

overhead of tracing is very small, because you confi gure a trace level only when needed.<br />

The tracing architecture has four major parts:<br />

➤<br />

➤<br />

➤<br />

➤<br />

T h e source is the originator of the trace information. You use the source to send trace messages.<br />

T h e switch defi nes the level of information to log. For example, you can request just error information<br />

or detailed verbose information.<br />

Tr a c e listeners defi ne where the trace<br />

messages should be written to.<br />

Listeners can have fi l t e r s attached. The<br />

fi lt er de fi ne s wh at t rac e m e s s a ge s shou ld<br />

be written by the listener. This way,<br />

you can have different listeners for the<br />

same source that write different levels of<br />

information.<br />

Figure 19 - 2 shows the major classes for tracing<br />

<strong>and</strong> how they are connected, in a Visual<br />

Studio class diagram. The TraceSource uses a<br />

switch to defi ne what information to log. The<br />

TraceSource has a TraceListenerCollection<br />

associated with it, which trace messages<br />

are forwarded to. The collection consists of<br />

TraceListener objects, <strong>and</strong> every listener has a<br />

TraceFilter connected.<br />

figure 19-2<br />

Several .<strong>NET</strong> technologies make use of trace sources that you just need to turn on<br />

to see what’s going on. For example, WPF defines sources among others with the<br />

names System.Windows.Data, System.Windows.RoutedEvent, System.Windows<br />

.Markup, System.Windows.Media.Animation. However, with WPF, you need to turn<br />

tracing on not only by confi guring listeners but also by setting within the registry key<br />

HKEY_CURRENT_USER\Software\MicrosoftTracing\WPF a new DWORD to the name<br />

ManagedTracing <strong>and</strong> the value 1.<br />

Classes from the System.Net namespace use the trace source System.Net; WCF<br />

uses the trace sources System.ServiceModel <strong>and</strong> System.ServiceModel.<br />

MessageLogging. WCF tracing is discussed in Chapter 43, “Windows Communication<br />

Foundation.”<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!