Enabling Tracing Programmatically in ASP.NET

By | January 2, 2011

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.

Author: 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 thoughts on “Enabling Tracing Programmatically in ASP.NET

  1. Pingback: Tweets that mention Enabling Tracing Programmatically in ASP.NET | Daily .Net Tips -- Topsy.com

  2. Pingback: Dew Drop – January 2, 2011 | Alvin Ashcraft's Morning Dew

Comments are closed.