In previous article we learnt how to enable tracing in WCF. In this article let’s learn how to enable tracing in ASP.net.Before we proceed with how to enable asp.net tracing we should have clear understanding of what ASP.net tracingis?
What is ASP.Net tracing?
A technic that allows us to understand what our application is/was doing so that we can debug or monitor the application. Tracing helps us to find out the defects in a web application using the diagnostic data provided thru tracing. Tracing in ASP.net allows us to view this data of every single request that is/was made for an ASP.Net page.
This diagnostic information contains run time data which contains execution path, session states, HTTP headers and other related information to diagnose the application.
Why Tracing in ASP.Net?
To understand why asp.net tracing is required we should know basic .Net application building terminology. There are two types of build configurations. One is ‘Debug‘ build and other one is ‘Release‘ build. A fact is that developer uses DEBUG builds and QA/production environment uses RELEASE builds. As name suggests Debug build contains in-built (coding level) debugging technics for troubleshooting purpose in development phase. While the Release builds usually doesn’t contain in-built debugging. We consider Release builds as more optimized than debug builds and hence improves the performance on production environment.
Developer uses .Net IDE to diagnose the defect in development phase (Debug build). In production environment (Release build) you won’t get .Net IDE to debug. And hence we need tracing in Release environment to trace the issue.
Confused between tracing and debugging, read-on more for better understanding –
What is the difference between Debugging and Tracing?
One liner statement can be when a web application is monitored in development phase it is termed as “Debugging” and when it is monitored in production environment it is termed as “Tracing”. Implementation of tracing in .net is a very easy task.
Tracing scopes and How to enable tracing in asp.net?
Following two tracing scopes are available in ASP.net. –
- Page Level Tracing
- Application Level Tracing
1) Page Level Tracing:
As name suggests, the scope of this asp.net trace is limited only to a page on which page level tracing is enabled. To enable the page level tracing, you would need to add “trace=true” directive in “.aspx” page.
- Open a “.aspx” page.
- Look at first line of the page, it would look like below –
<%@ PageTitle=”Home Page” Language=”C#” MasterPageFile=”~/Site.master” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
- Add directive “trace” and set its value to “true” as shown below –
<%@ PageTitle=”Home Page” Language=”C#” MasterPageFile=”~/Site.master” AutoEventWireup=”true” Trace=”true”CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
Observe the directive highlighted in green.
- Now execute your web application and open a page on which you have enabled page level tracing. Tracing information would appear at bottom of the page in browser as shown below –
Tracing in Asp.net
- Observe the events in “Message” column in above image. It shows all events of a page.
2) Custom Page Level Tracing:
- In above steps we enabled tracing in a page and it showed all events in tracing output. It is also possible to add custom tracing information at page level. To add custom tracing information import following namespace in (.aspx.cs) page –
This namespace is used to implement Debug and Tracing in asp.net.
- By using “Debug” and “Trace” methods we can track the custom information. For ex, look at below code –
protected voidPage_Load(object sender, EventArgs e)
// Custom Debug message.
Debug.WriteLine(“You are in Debug Mode.”);
// Custom Trace message.
Trace.Write(“You are in Trace Mode.n”);
In Page_Load event we are tracking page load event in both debug and trace mode.
- Now execute the web application and stay in .net IDE.
- Now go to “Debug -> Windows -> Output” menu item as shown in below image. It would open up an “Output” window to see the debugging messages.
[Note :] Debugging should be enabled to appear debug message in output window.
Output Window for Debugging
- A debugging message i.e. “You are in Debug Mode.” would appear in output window as shown below –
Output Window – Showing Debug messages
Now look at the “Output” window again, it would show only Debug message that we added, however it would not show trace message.
- Now go to a browser where your page is opened and look at the bottom of the page, you would see trace message that we added thru above code as shown in below image –
Asp.net tracing on Browser
Observe the debug message is not appeared in trace information.
Debug code never get include in final version on production environment hence the Debug message would not appear in tracing.
3) Application Level Tracing:
Above we have seen that how to enable tracing in asp.net on page level. In this we need to add “trace” directive in all pages where we want tracing to be enabled.
In application level tracing we can add trace directive in a single place and then it would be applicable to all pages throughout the web application, eliminating to add directive in multiple pages.
To enable the web tracing in application level you would need “web.config” configuration file. We all are aware that this configuration file is used to manage the settings of web application. To enable the tracing in application level add below lines in your web.config file under <system.web> –
Now execute web application and you would see the tracing information for all pages.
In above all example the tracing information is coming out at the bottom of the page in browser, hence making it very bad idea with respect to user experience.
To make it very user friendly we can avoid this situation by setting “pageOutput” attribute to “false” as shown below –
Now when you execute the web application you would observe that the tracing information is not showing at bottom of the page. But then how to see the tracing?
To see the tracing information you would need to access the “Trace.axd” of your web application. Following is the example to access the “Trace.axd” on your browser –
It would like below screenshot –
|Asp.net Tracing with Trace.axd|
Click on “View Details” link and it would open complete tracing information for that page.
How to persist the tracing information in Text File?
In above example we have seen that the tracing information only gets appeared on web browser. What if you want to persist the information in a simple physical text file? Well, in that case you would need to add listener in configuration file. Look at below code, we have added listener to write tracing in text file to enable tracing in asp.net –
Add/update below lines in configuration file.
Here writeToDiagnosticsTrace with “true” makes sure that the tracing should get write on physical file.
Now add below lines in configuration file.
Now execute the web application and open your desired page. It would write the tracing information in a file highlighted in yellow.
Tracing is most important aspect of the ASP.net for example –
- If you have created a web application to send an email using asp.net and if something goes wrong on deployment then you can take the help of tracing for debugging the issue.
- If you have a asp.net web service and it stopped working on deployment then you can use tracing to identify the issue.
I hope you enjoyed the article. If yes then please feel free to share the article on your favourite social media using below “Sharing is Caring” widget.
More articles –