How to do it...

  1. To start off, go to ToolsOptions.
  2. Expand the IntelliTrace node and click on General. Ensure that Enable IntelliTrace is checked. Also, make sure that the IntelliTrace events and call information option is selected. Click on OK:
  1. In the Recipes.cs file, you might need to add the following using statements:
        using System.Diagnostics; 
using System.Reflection;
using System.IO;
  1. Add a method called ErrorInception() to the Recipes class. Also, add the code to read the base path and assume that there is a folder called log. Do not create this folder on your hard drive. We want an exception to be thrown. Lastly, add another method called LogException() that does nothing:
        public static void ErrorInception() 
{
string basepath = Path.GetDirectoryName(
Assembly.GetEntryAssembly().Location);
var full = Path.Combine(basepath, "log");
}

private static void LogException(string message)
{

}
  1. Add the following code to your ErrorInception() method after the full path has been determined. Here, we are trying to open the log file. This is where the exception will occur:
        try 
{
for (int i = 0; i <= 3; i++)
{
// do work
File.Open($"{full}log.txt", FileMode.Append);
}
}
catch (Exception ex)
{
StackTrace st = new StackTrace();
StackFrame sf = st.GetFrame(0);
MethodBase currentMethodName = sf.GetMethod();
ex.Data.Add("Date", DateTime.Now);
LogException(ex.Message);
}
  1. When you have added all your code, your code should look like this:
        public static void ErrorInception() 
{
string basepath = Path.GetDirectoryName(
Assembly.GetEntryAssembly().Location);
var full = Path.Combine(basepath, "log");

try
{
for (int i = 0; i <= 3; i++)
{
// do work
File.Open($"{full}log.txt", FileMode.Append);
}
}
catch (Exception ex)
{
StackTrace st = new StackTrace();
StackFrame sf = st.GetFrame(0);
MethodBase currentMethodName = sf.GetMethod();
ex.Data.Add("Date", DateTime.Now);
LogException(ex.Message);
}
}

private static void LogException(string message)
{

}
  1. In the Program.cs file, call the ErrorInception() method. Right after that, do a Console.ReadLine() so that our console application will pause there. Do not add any breakpoints anywhere to your code:
        ErrorInception(); 
Console.ReadLine();
  1. Start debugging your application. The exception is thrown and the application continues running, a condition often experienced with much more complex applications. At this point, you would expect a log file to be appended with the fictitious data of the app, but nothing happened. It is, at this point, that you stop your application and start adding breakpoints all over your code in a hit and miss-type exercise. I say hit and miss because you probably won't know exactly where the error is. This is especially true if your code file contains a few thousand lines of code. Well now with IntelliTrace and historical debugging, you just need to click on the Break All button:
  1. Your application is now essentially paused. If you don't see the Diagnostic Tools window, hold down Ctrl + Alt + F2.
  1. Visual Studio now displays the Diagnostic Tools window. Immediately, you can see that there is a problem indicated by the red diamond icon on the Events section. In the Events tab at the bottom, you can click on the exception:
  1. Doing this expands the exception details where you can see that the log file was not found. Visual Studio, however, goes one step further with historical debugging:
  1. You will see a link at the bottom of the exception details that says Activate Historical Debugging. Click on this link. This allows you to see the actual line of code that caused this exception in the code editor. It also allows you to view the history of the applications state in the Locals window, call stack, and other windows. You can now see the specific line of code that caused the exception in your code editor. In the Locals window, you can also see what the path was that the application used to look for the log file. This kind of debugging experience is immensely powerful and allows developers to go straight to the source of the error. This leads to increased productivity and better code:
..................Content has been hidden....................

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