ASP.NET Web Site Debugging Part 2 Debug Output
This tutorial is the second part of the ASP.NET Web Site Debugging tutorial series. This tutorial will demonstrate how to use the Debug Output Window in Microsoft Visual Studio 2010 with ASP.NET 4.0 and C#.
When debugging your websites, sometimes you might want to output a message after a certain line of code to gather some valueable data about what is going on in the background. This can be useful to see values and other information all in one convenient place, while only adding a small amount of code to do so.
Creating a Web Site
To demonstrate the debug output window, we will need to make a simple website that allows us to add some debug code. At this point, I have created a new ASP.NET Empty Web Site. Next, we will add a Web Form. To do this:
- Right click the project in your Solution Explorer.
- Select Add New Item…
- Choose a Web Form.
- Name it Default.aspx.
- Click Add.
We are going to use a Label to write some simple test code for our debugging purposes. To do this, open Default.aspx up to Design mode and drag and drop a Label Control onto the Web Form leaving the default properties.
Adding the Debug Code
Next, we need to open the Default.aspx.cs up for editing so that we can add in some debug code here. First, we need to include System.Diagnostics which will allow us access to the debug output. To do this, at the top of the Default.aspx.cs class add the following using statement:
Next, we need to add the following code to the Page_Load event method:
protected void Page_Load(object sender, EventArgs e)
//Set the text to Label1
Label1.Text = "Hello Label1!";
//Write the current value of Label1 to the debugger
Debug.WriteLine("Label1.Text = " + Label1.Text);
//Write a success message if a query string is found
Debug.WriteLineIf((!string.IsNullOrEmpty(Request.QueryString["ID"])), "ID Query String Was Found!");
Let’s review what this code is doing. First, we set the text of the Label we added earlier. Then, we begin to write code that will output to the debug output window. Here we are using two different methods. First, the WriteLine method will simply write a message to the output window. Second, the WriteLineIf method which accepts two parameters, a condition which will determine if a message is output, and then a message to output.
When we load this website up with debugging, we should first see a message that says “Label1.Text = Hello Label1!” and then based on the condition if a query string named id is passed, a message saying “ID Query String Was Found!”.
Let’s go ahead and test this out to make sure that our output is correct. To do this, start the website with debugging and check the output in the Ouput window ensuring that the ‘Show output from’ DropDownList has Debug selected. (Note: If your Output window is currently not being shown, you can simply enable it from the top menu by selecting View -> Output or hitting the Ctrl + Alt + O hotkey.) When this runs you should see the first message we output, “Label1.Text = Hello Label1!”, but not the second. This is because we have not added a query string to the URL of the page. To test this, add a ‘?id=1’ at the end of the URL in the address bar and reload the page.
Using this output window effectively can greatly help you figure out what is going on in the background of your code by outputting any values that you have access to during runtime. This is great if you need to follow variables while they are being changed multiple times to ensure that everything is working correctly.
Download Source Files