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.
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
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:
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:
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
Next, we need to add the following code to the Page_Load event method:
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!".
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
Testing
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.

Now you should see output similar to this:

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.
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
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.
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:
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
using System.Diagnostics; |
Next, we need to add the following code to the Page_Load event method:
protected void Page_Load(object sender, EventArgs e) |
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!".
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
Testing
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.

Now you should see output similar to this:

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.
No comments:
Post a Comment