Tracing and debugging feature in visual CSharp
Tracing and debugging feature in visual CSharp enables us to provide information regarding the performance of our application right from few lines of code (LOC) written, to the final deployment of the application.The Debug and Trace classes in .Net Framework provide this functionality to use tracing and debugging methods.
These methods use the ConditionalAttribute to designate when they should be included.
The classes for Debug and Trace are: System.Diagnostics.Debug and System.Diagnostics
What is tracing ?
In the field of programming, tracing is a way to track what is happening with the software when it is used.Either it is giving the proper result or is it causing some errors in the program? Tracing means just highlighting the tracked down areas in the program. It is not as detailed as debugging is
What is debugging ?
Debugging is mostly used in the environment where the whole team of software developers want more details about almost any aspect of programming. Hence, debugging seems to be more focused towards
the rich details as compared to tracing
The easiest example will move us towards the actual usage of these classes.
/* Single threaded apartment*/
static void Main(string args)
string Name = “Adeel”;
double Height = 6.1;
string MaritalStatus = “Unmarried”;
/*Don’t expect the output from the Debug class here.All these outputs will be
captured in the file and then they will be retrieved */
Debug.WriteLine(“My Bio-data “);
Debug.WriteLine(“My name is: ” + Name);
Debug.WriteLine(“My height is: ” + Height.ToString());
Debug.WriteLine(“My marital status is : ” + MaritalStatus);
Debug.WriteLineIf(Height> 6.0 , “Good height”);
Debug.WriteLineIf(MaritalStatus.ToLower()== “Unmarried”, “Unmarried”);
TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out);
/* Directs the debug output to the stream such as file.In our case, we have file at
D: named TextFile on which, the whole Debug trace has been written*/
TextWriterTraceListener tr2 = new TextWriterTraceListener(System.IO.File.CreateText(@”D:TextFile.txt”));
/* This listener is added to our Debug class to capture the contents of the created file*/
Trace.WriteLine(“Tracing is in action “);
Trace.WriteLine(“My Bio-data “);
Trace.WriteLine(“My name is: ” + Name);
Trace.WriteLine(“My height is: ” + Height.ToString());
Trace.WriteLine(“My marital status is : ” + MaritalStatus);