Tracing

Tracing enables us to monitor an application while it is executing. When a program is executed, the runtime allows us to write messages to monitor the control flow of the program. This will enable us to identify any incorrect behavior of the application logic. In the case of exceptions, we can identify where exactly the code failed and what variable values caused issues in smooth execution. These messages can be created by the System.Diagnostics.Debug class. When such messages are created, by default, these messages are displayed in the output window of Visual Studio.

Apart from creating these messages, you can redirect these messages to a file or database using the System.Diagnostics.Trace class. You can register listeners using the trace class, which allows you to redirect your messages. Here, your debug class or trace class acts as the publisher and the listener class acts as a subscriber. We hope you remember Chapter 5, Creating and Implementing Events and Callbacks, where we learned about the publisher and subscriber model.

Let's take a look at an example to understand how we can use debug messages. In the following program, we are trying to accept two input parameters and perform actions such as addition and subtraction on those numbers. However, we added a few extra lines to monitor messages that record what's happening:

internal void Method4()
{
Console.WriteLine("Enter a numeric value");
int number1 = Convert.ToInt32(Console.ReadLine());
Debug.WriteLine($"Entered number 1 is: {number1}");

Console.WriteLine("Enter another numeric value");
int number2 = Convert.ToInt32(Console.ReadLine());
Debug.WriteLine($"Entered number 2 is: {number2}");

int number3 = number1 + number2;
Debug.WriteLineIf(number3>10, $"Sum of number1 & number 2 is : {number3}");
int number4 = number1 - number2;
Debug.WriteLineIf(number4 < 10, $"Difference of number1 & number 2 is : {number4}");
}

Because we used Debug.WriteLine to record the messages, these values are written in the output window. Observe the following output window where all Debug.WriteLine messages are written:

In the preceding code block, you can see the last two Debug.WriteLine statements in the program block, where Debug.WriteLineIf is used. The system checks the condition that we provided, and, if it returns true, the system writes the message to the output window.

Now, let's go a step further and see how we can use tracing listeners to redirect your message to different channels.

We are going to use the same program with five extra lines, where we add Console.Out and a logfile.txt file as two different trace listeners, and then attach these two listeners to the debug object. The last line is Debug.Flush, which pushes all messages from the object to the log file:

internal void Method5()
{
TextWriterTraceListener listener1 = new TextWriterTraceListener(Console.Out);
Debug.Listeners.Add(listener1);

TextWriterTraceListener listener2 = new TextWriterTraceListener(File.CreateText("logfile.txt"));
Debug.Listeners.Add(listener2);

Console.WriteLine("Enter a numeric value");
int number1 = Convert.ToInt32(Console.ReadLine());
Debug.WriteLine($"Entered number 1 is: {number1}");

Console.WriteLine("Enter another numeric value");
int number2 = Convert.ToInt32(Console.ReadLine());
Debug.WriteLine($"Entered number 2 is: {number2}");

int number3 = number1 + number2;
Debug.WriteLineIf(number3 > 10, $"Sum of number1 & number 2 is : {number3}");
int number4 = number1 - number2;
Debug.WriteLineIf(number4 < 10, $"Difference of number1 & number 2 is : {number4}");
Debug.Flush();
}

Because we added Console.Out as one of the listeners, Debug.WriteLine messages are now written on the screen when we execute the program:

Additionally, as we added logfile.txt as one of the listeners, a new text file gets created in the program executing folder where Debug.WriteLine messages are written:

These listeners are not limited to text files and console. Based on the project requirement, you can add XML, database listeners, which might require some extra coding.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.253.198