Enabling Tracing Programmatically in ASP.NET

Enabling Tracing Programmatically in ASP.NET

ASP.NET provides a internal details of page related information by enabling Tracing. To use tracing, we need to explicitly enable it from Page directives as shown in below

image

Enable tracing from page directives is one of the easiest way to enable it. But, we can use Page.Trace object to enable or disabled the tracing programmatically.

protected void Page_Load(object sender, EventArgs e)
 { 
//check for null query string as well
if (Request.QueryString["EnableTracing"].Equals("true")) 
{
 Trace.IsEnabled = true;
 }
 }

Trace object is an Instance of TraceContextClass which is actually control all the operation related with Tracing.This is extremely useful when you want to enable or disable tracing for a page based on some condition or query string.

protected void Page_Load(object sender, EventArgs e)
 {
//check for null query string as well
 if (Request.QueryString["EnableTracing"].Equals("true") && UserRole.Role == "Admin") 
{
 Trace.IsEnabled = true;
 } 
} 

So, now you can enable or disable Tracing whenever you want.

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.

2 Comments to “Enabling Tracing Programmatically in ASP.NET”

Comments are closed.