Tracing and debugging features


Tracing and debugging feature in visual CSharp
logo

image credit:blogs.msdn.com

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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace TracingandDebugging
{

class myClass
{

/* Single threaded apartment*/
[STAThread]
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.Indent();
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”);

Debug.Flush();

TextWriterTraceListener tr1 = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(tr1);

/* 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*/
Debug.Listeners.Add(tr2);

Trace.WriteLine(“Tracing is in action “);
Trace.Indent();

Trace.WriteLine(“My Bio-data “);
Trace.Indent();
Trace.WriteLine(“My name is: ” + Name);
Trace.WriteLine(“My height is: ” + Height.ToString());
Trace.WriteLine(“My marital status is : ” + MaritalStatus);

Trace.Unindent();

Trace.Flush();

Console.ReadLine();
}
}
}

Output

output

Syed Adeel Ahmed
Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.

Leave a Reply