Scrigroup - Documente si articole

     

HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

The TraceSwitch Class

c



+ Font mai mare | - Font mai mic



The TraceSwitch Class

This class is an enhanced version of the BooleanSwitch class because it allows us to choose whether to deactivate tracing functionality or display messages using an importance-based hierarchy. The following table lists the levels:



Trace level

Description

None: Tracing is deactivated.

TraceError: Only the error messages will be written to the listener application.

TraceWarning: Error and warning messages will be written to the listener application.

TraceInformation: Error, warning, and information messages will be written to the listener application.

TraceVerbose: All kinds of messages will be written to the listener application.

So, when an error occurs, you can change the application configuration file, specifying to write just the error messages that you have added to the code to focus our attention just on these messages. The configuration file is the same as for the BooleanSwitch example. What changes is the code, as we have to instantiate an object from the TraceSwitch class. Moreover, we will use the enumeration values within the class to specify the level of the tracing messages. Let's see an example, TraceSwitchExample.cs:

using System;
using System.Threading;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;

namespace TraceSwitchExample

We start by declaring a global TraceSwitch object and then create a new object, giving it the name specified in the configuration file. We add a text file log listener to the application. We then start a new thread that contacts the pubs database within SQL Server in order to retrieve all the records from the authors table.

If the thread has been omitted, the Open() method raises an exception that generates a trace error message:

private static void DBThread()


catch (Exception ex)

finally


Here is the output from the code when the value 1 is specified in the configuration file, which specifies TraceError:

19/04/2002 17:52:23 - Error: ExecuteReader requires an open and available Connection. The
connection's current state is Closed.
Slept for 1 second.

Here is the output when the value 3 is specified in the configuration file, which specifies TraceInformation:

19/04/2002 17:54:23 - Entered Main()
19/04/2002 17:54:23 - Entered DBThread()
19/04/2002 17:54:24 - Executing SQL statement
19/04/2002 17:54:24 - Error: ExecuteReader requires an open and available Connection. The
connection's current state is Closed.

The Debug Class

The Debug class provides the same functionality as the Trace class. You will find that it exposes the same methods and properties, with the same tracing results.

When you change the listener application using the Listeners collection provided by the Trace class, you will change the listener application for Debug messages as well.

The big difference between these two classes is in the context in which you should use them. The Debug class is useful when you need to add information during debugging sessions. Before deploying our application, you will build the release version that will remove debug information from our code, automatically. Therefore, you would add Trace class functionalities when you need to check our application during the run time phase.



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 710
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved