|Access||Adobe photoshop||Algoritmi||Autocad||Baze de date||C||C sharp|
|Calculatoare||Corel draw||Dot net||Excel||Fox pro||Frontpage||Hardware|
|Php||Power point||Retele calculatoare||Sql||Tutorials||Webdesign||Windows|
|Asp||Autocad||C||Dot net||Excel||Fox pro||Html||Java|
We are going to conclude our listener explanation by examining the TextWriterTraceListener class. It is useful when you have to write our tracing messages to a text file or directly in a console application. In fact, during the creation of a TextWriterTraceListener object, you can specify either a TextWriter object, or a Stream object. Using a Stream object allows you to specify more details on how the file stream is handled. The following snippet of code, TraceConsole.cs, shows how to trace messages in a Console application:
Specifying the Console.Out streaming in the class's constructor, our Console application will display tracing messages:
Finally, let's see how to add text log files as listener. We have to add a new TextWriterTraceListener object, specifying a FileStream object in its constructor. When the application ends, you have to use the static Close() method provided by the Trace class in order to close the log writing all the tracing messages. In the following code, Debugging.cs, a thread is started that traces both main and secondary thread messages:private static void WritingThread()
The WritingThread() method is simply used by the thread to sleep for a second and write some tracing messages.
Here, we create a new FileStream object, either creating or opening the Debugging.log file, if it already exists. Then, we add the new listener into the Listeners collection by creating a new instance of the TextWriterTraceListener class within the Add() method:static void
The output of the code will be something similar to this:30/04/2002 16:38:15 - Entered
The Trace class provides a useful property called IndentLevel for indenting tracing messages. For instance, you could use different indent levels for tracing messages written by the main and secondary threads. Adding the following lines to the code above, we can accomplish this task easily:
The output of the modified code is:30/04/2002 16:40:07 - Entered
You can increment or decrement the level of the indentation using the Indent() and Unindent() methods, respectively.
When you are near to the application deployment phase, you will probably want to remove all the tracing and debugging messages from the code. However, you do not have to look for every trace instruction and remove it. You can use compilation flags during the application building. From the Visual Studio .NET IDE, you can right-click on the project name within the Solution Explorer window, selecting the Properties item from the context menu. The following dialog box will appear:
You simply need to uncheck the Define DEBUG constant and Define TRACE constant checkboxes, recompile the solution, and all the Trace and Debug statements will be stripped from the application.
In order to remove tracing functionalities, you can even use the csc.exe command-line compiler. Simply use the /d:TRACE=FALSE /d:DEBUG=FALSE switches when compiling.
Adding switches to the traced code allows us to activate/deactivate tracing messages at run time. By simply declaring a value in the configuration file of our application, you can activate the trace functionality without rebuilding the entire solution. Naturally, you have to build the application to maintain tracing information, and this results in a greater final application size and slower performance, even when the switches are turned off.
The BooleanSwitch and TraceSwitch classes are provided by the .NET Framework to implement these switches. Let's first examine the BooleanSwitch class.
Politica de confidentialitate|
Adauga cod HTML in site