Sometimes you may require to monitor variables, methods in your Asp.Net web application. You can do this by using Trace class in Asp.Net. Tracing is the another debugging method in Asp.Net. In this article we discuss about Tracing with simple example.
Open Microsoft Visual Studio => Create New Asp.net Web Application. We have to enable Tracing by using Trace attribute for the web page in Page directive as shown below.
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASPNETTracing._Default" Trace="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Default.aspx.cs
Write code to generate the factorial number as shown below in code-behind file.
using System;
namespace ASPNETTracing
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int iFactorial = 1;
for (int i = 1; i <= 5; i++)
{
iFactorial *= i;
}
}
}
}
Execute the application and the output is as shown below.
If you want you can write content to Tracing by using Trace class as shown below.
using System;
namespace ASPNETTracing
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int iFactorial = 1;
for (int i = 1; i <= 10; i++)
{
iFactorial *= i;
}
Trace.Write("Actual input value : 10");
Trace.Write("Result Factorial Number : " + iFactorial.ToString());
}
}
}
Execute the application and custom content is written to tracing as shown below.
There are six sections in Tracing as shown above. Those are Request Details, Trace Information, Control Tree, Cookies Collection, Headers Collections, Forms Collection and Server Variables.
Request Details describes the information pertaining to the request, Trace Information contains the info about Application currently running. Control Tree is used to display the info about Asp.Net Controls used and ViewState size of the page. Cookies Collection contains all cookies used in the application, Header Collection is used to displays the HTTP header information like content length, User Agent...etc. Forms Collection is used to displays the name of the Asp.Net controls and its values used in the application. Server Variables section is used to display the Environment variables used on the Server Side.
Trace class has properties IsEnabled, TraceMode. IsEnabled is used to enable the tracing for current request or not. TraceMode has two values SortByCategory and SortByTime.
Trace class also has two methods Warn and Write, Warn method writes the trace information in red and Write method writes the information in normal way.
We can write trace information by using Warn and Write methods as shown below.
using System;
namespace ASPNETTracing
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int iFactorial = 1;
for (int i = 1; i <= 10; i++)
{
iFactorial *= i;
}
Trace.Warn("Actual input value : 10");
Trace.Write("Result Factorial Number : " + iFactorial.ToString());
}
}
}
Execute the application and the output is as shown below.