Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Instrumenting Applications with .NET Tracing : Page 2

If you really want to know how a production application is running, try turning on .NET's tracing capabilities. You can use .NET's built-in tracing or build your own.


advertisement
Basic Tracing
For this article we'll use the Trace class to show how to use .NET types for instrumentation of production systems. While I wrote the examples in this article as Console applications for simplicity, you can use this information for instrumenting any kind of .NET application, including ASP.NET, remoting, Web services, and Windows Forms. The Trace class has several members that control output, which you can see in the following code.

using System; using System.Diagnostics; class BasicTracing { static void Main() { // specify where trace output should go Trace.Listeners.Add( new TextWriterTraceListener(Console.Out)); // category tag for output string category = "Basic Tracing"; // just write, no newline Trace.Write("Trace.Write(); ", category); // write with a newline Trace.WriteLine("Trace.WriteLine(); ", category); // write if condition is true, no newline Trace.WriteIf(true, "Trace.WriteIf(); ", category); // write with newline if condition is true Trace.WriteLineIf(true, "Trace.WriteLineIf(); ", category); // raise assertion alert if condition is false Trace.Assert(false, "Trace.Assert(); ", "Shown when condition is false"); // unconditionally raise assertion alert Trace.Fail("Trace.Fail(); ", "Shown unconditionally."); Console.ReadLine(); } }

When your tracing needs are simple, that is, you only need to turn tracing on or off, a Boolean switch is a good choice.

The first line in the preceding code adds a TraceListener to the Listeners collection. TraceListeners are discussed in the Trace Listeners section of this article. They let you specify where the output of write statements will go.

The WriteLine and Write methods write output with and without a newline, respectively. The WriteLineIf and WriteIf methods write output if a specified condition evaluates to true. The category parameter enables you to prefix the output with a tag string, which is useful for filtering log entries.

The Assert method displays a dialog and writes output when the specified condition evaluates to false. The condition is explicitly set to false above to raise the assertion. You normally use the Assert method with the Debug class so you can test assumptions in your code during development. You don't normally want assertion message boxes popping up in front of users.

The Fail method raises an assertion dialog and writes to output unconditionally. Although the preceding example code shows the Fail statement with the Trace class, you should use it with the Debug class because you don't want a message box popping up in front of your users. You can use the Fail method when you reach an illogical segment of code such as a default in a switch statement that doesn't make sense or in an exception catch block. The benefit of the Fail method is that you can press the Retry button and break into the debugger to determine the cause of the problem.

The Trace class also includes other members for controlling output, including properties for indentation, output flushing, and closing listeners. Their implementations are straight forward and I refer you to the documentation for more information.

The next two sections discuss switches, which you use for conditional output with the WriteIf and WriteLineIf methods.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date