Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Continuing our quest to master the debugging tools in Visual Studio, I am going to cover trace points, attaching a debugger to a running process, and debugging a web site. So let's get started! I am inside the solution called OtherDebuggingTechniques, and it contains two projects: AttachToConsole and DebugWebApp. I am going to start by showing you something called a tracepoint. To do that, I am going to double-click on this Program.cs. If you've watched the other movies in this chapter, you know that you can add a breakpoint by clicking in this margin or pressing F9.
A breakpoint is hit if you're attached to it debugger, and you run this line of code. It will then stop and let you look at your code. A tracepoint is similar to a breakpoint. When you establish a tracepoint, you also specify additional action that can be taken when it is hit. Let me show you. I am going to right-click on this breakpoint and choose When Hit. Now, it doesn't actually say the word "tracepoint" in here, but this is where you would enable a tracepoint. I click here, and then I get to choose what happens when the debugger encounters this tracepoint.
First of all, I can have an output information to the Output window. I can have it do this silently. If this Continue execution check mark is checked, then it'll print this output to the Output window, but it won't stop and let me look at the code. Let me add a custom string at the front of this, so we can find it easily, and then this is going to print the function name, and this is going to print the thread ID and the thread's name. As you can see, there are other variables I can inject in there. I can also do other actions like run macros, and macros are covered in another chapter in this title.
Macros are little bits of code that you can run from within Visual Studio. I am just going to do this basic tracepoint. Make sure I uncheck this Run a macro first. Click on OK. Notice it has a diamond now instead of a circle? That's how you know it's a tracepoint. Then I am going to do a Debug > Start Debugging. It ask me to enter my name, it says, "Hello Walt," and now if I come over here to my Output window and scroll down to the end-- you may not see this window in your computer; if you don't, you need to go to View > Output--and here at the bottom of the window is my custom output string.
Notice that it didn't stop my code I am running. So that's the tracepoints. Another common task that you might want to do when you are debugging is you might need to have an application already running before you attach a debugger. In other words, you don't want to attach the debugger at the Startup Process. Or another thing that's happening is you're running an application and five or six hours after you started running it, something happens and it doesn't look right, and at that point, you want to start attaching a debugger and taking a look. So, what I am going to do is I am going to run this AttachToConsole application directly.
To do that, I am going to right-click, choose Open Folder in Windows Explorer, open my bin folder and my Debug folder, and run this AttachToConsole. Let me show you how it works. I'll type in my name, and there is the same code we saw before. Now, it's sitting there waiting for my input. I can go back to Visual Studio, go to my Debug menu, and say attach to process.
This dialog shows me all of the running processes on my computer. I am going to look for one called AttachToConsole.exe. That's the one that I am currently running. And then I am going to click Attach. F9 put their breakpoint here, and then I will go back here and press the Enter key, which cause the next line of code to run. And as you can see it now hit my breakpoint. And now at this point, I can do any of my normal debugging task.
I can take this yellow marker and move it to a previous line, I can examine the data in the variables, and so on. For my next demonstration, I want to show you how to debug an ASP.NET web site. So let me close this application by clicking this Stop button, and then I am going to go over to my solution and right-click on the DebugWebApp and tell it to make that my StartUp object. Now, this is a ASP.NET application. So, one of the things that you have to do to make an ASP.NET application debuggable is add a line of code to your Web.config.
By default, Visual Studio does that automatically for you when you create a web project. If it doesn't have a debugging setting, then it'll prompt you and say, would you like to modify the Web.config? So, I have already done that on this application. So all I should have to do is open up this window here, and I'll show you the bit of code I have here. I have an asp:Button in here that has a Button1_Click procedure. And if I press F7, you can see the code behind. This is the ASP page.
So we have got two breakpoints, right? I need to attach a debugger, so I will choose Debug > Start Debugging. Visual Studio will launch the ASP.NET development server, and then it will launch my web browser, and it attaches the browser to this instance of Internet Explorer. Now, when I click on this button, you'll see that it switches me over to the code behind, and I can hover over the variables here. I can press F11.
All in all, I'd say the Visual Studio contains a nice selection to debugging tools. In the next movie, I will show you the IntelliTrace tool, which changes the way you think about debugging.
Get unlimited access to all courses for just $25/month.Become a member