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.

468 ❘ ChaPTer 19 instrumentAtiOn<br />

Trace sources<br />

You can write trace messages with the TraceSource class. Tracing requires the Trace fl ag of the compiler<br />

settings. With a Visual Studio project, the Trace fl ag is set by default with debug <strong>and</strong> release builds, but you<br />

can change it through the Build properties of the project.<br />

T he TraceSource class is more diffi cult to use compared to the Trace class writing<br />

trace messages, but it provides more options.<br />

To write trace messages, you need to create a new TraceSource instance. In the constructor, the name of<br />

the trace source is defi ned. The method TraceInformation() writes an information message to the trace<br />

output. Instead of just writing informational messages, the TraceEvent() method requires an enumeration<br />

value of type TraceEventType to defi ne the type of the trace message. TraceEventType.Error specifi es the<br />

message as an error message. You can defi ne it with a trace switch to see only error messages. The second<br />

argument of the TraceEvent() method requires an identifi er. The ID can be used within the application<br />

itself. For example, you can use id 1 for entering a method <strong>and</strong> id 2 for exiting a method. The method<br />

TraceEvent() is overloaded, so the TraceEventType <strong>and</strong> the ID are the only required parameters.<br />

Using the third parameter of an overloaded method, you can pass the message written to the trace.<br />

TraceEvent() also supports passing a format string with any number of parameters in the same way as<br />

Console.WriteLine() . TraceInformation() does nothing more than invoke TraceEvent() with an<br />

identifi er of 0. TraceInformation() is just a simplifi ed version of TraceEvent() . With the TraceData()<br />

method, you can pass any object, for example, an exception instance, instead of a message. To make sure<br />

that data is written by the listeners <strong>and</strong> does not stay in memory, you need to do a Flush() . If the source<br />

is no longer needed, you can invoke the Close() method that closes all listeners associated with the trace<br />

source. Close() does a Flush() as well.<br />

public class Program<br />

{<br />

internal static TraceSource trace =<br />

new TraceSource("Wrox.ProCSharp.Instrumentation");<br />

static void TraceSourceDemo1()<br />

{<br />

trace.TraceInformation("Info message");<br />

}<br />

trace.TraceEvent(TraceEventType.Error, 3, "Error message");<br />

trace.TraceData(TraceEventType.Information, 2, "data1", 4, 5);<br />

trace.Flush();<br />

trace.Close();<br />

code snippet TracingDemo/Program.cs<br />

You can use different trace sources within your application. It makes sense to defi ne<br />

different sources for different libraries, so that you can turn on different trace levels<br />

for different parts of your application. To use a trace source, you need to know its name.<br />

A commonly used name for the trace source is the same name as the assembly name.<br />

The TraceEventType enumeration that is passed as an argument to the TraceEvent() method<br />

defi nes the following levels to specify the severity of the problem: Verbose , Information , Warning , Error ,<br />

<strong>and</strong> Critical . Critical defi nes a fatal error or application crash; Error defi nes a recoverable error. Trace<br />

messages at the Verbose level give you detailed debugging information. TraceEventType also defi nes<br />

action levels Start , Stop , Suspend , <strong>and</strong> Resume . These levels defi ne timely events inside a logical operation.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!