Tracing in ASP.NET
page 4 of 4
by Phil Winstanley
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 37075/ 88

Tidy the Mess

The easiest and quickest way to get around these limitations of Trace is to encapsulate all the functionality so that we can call it in one line. One way to do this is to create a class such as so.

using System;

namespace My

{

          public class Trace

          {

                   public static void Write(string Message)

                   {

                             if (System.Web.HttpContext.Current.Trace.IsEnabled)

                             {

                                      try

                                      {

                                                System.Web.HttpContext.Current.Trace.Write(Message.ToString());

                                      }

                                      catch(Exception e)

                                      {

                                                System.Web.HttpContext.Current.Trace.Warn("Intranet.Web","Write",e);

                                      }

                             }

                   }

                   public static void Write(string Category, string Message)

                   {

                             if (System.Web.HttpContext.Current.Trace.IsEnabled)

                             {

                                      try

                                      {

                                                System.Web.HttpContext.Current.Trace.Write(Category.ToString(),Message.ToString());

                                      }

                                      catch(Exception e)

                                      {

                                                System.Web.HttpContext.Current.Trace.Warn("Intranet.Web","Write",e);

                                      }

                             }

                   }

                   public static void Write(string Category, string Message, System.Exception exe)

                   {

                             if (System.Web.HttpContext.Current.Trace.IsEnabled)

                             {

                                      try

                                      {

                                                System.Web.HttpContext.Current.Trace.Write(Category.ToString(),Message.ToString(),exe);

                                      }

                                      catch(Exception e)

                                      {

                                                System.Web.HttpContext.Current.Trace.Warn("Intranet.Web","Write",e);

                                      }

                             }

                   }

                   public static void Warn(string Message)

                   {

                             if (System.Web.HttpContext.Current.Trace.IsEnabled)

                             {

                                      try

                                      {

                                                System.Web.HttpContext.Current.Trace.Warn(Message.ToString());

                                      }

                                      catch(Exception e)

                                      {

                                                System.Web.HttpContext.Current.Trace.Warn("Intranet.Web","Write",e);

                                      }

                             }

                   }

                   public static void Warn(string Category, string Message)

                   {

                             if (System.Web.HttpContext.Current.Trace.IsEnabled)

                             {

                                      try

                                      {

                                                System.Web.HttpContext.Current.Trace.Warn(Category.ToString(),Message.ToString());

                                      }

                                      catch(Exception e)

                                      {

                                                System.Web.HttpContext.Current.Trace.Warn("Intranet.Web","Write",e);

                                      }

                             }

                   }

                   public static void Warn(string Category, string Message, System.Exception exe)

                   {

                             if (System.Web.HttpContext.Current.Trace.IsEnabled)

                             {

                                      try

                                      {

                                                System.Web.HttpContext.Current.Trace.Warn(Category.ToString(),Message.ToString(),exe);

                                      }

                                      catch(Exception e)

                                      {

                                                System.Web.HttpContext.Current.Trace.Warn("Intranet.Web","Write",e);

                                      }

                             }

                   }

          }

}

If you place the above in to a class file in your project, you can then use it as a 1 line replacement for Trace as so: -

private void Page_Load(object sender, System.EventArgs e)

{

          if (Request.QueryString["Doesn'tExist"] != null)

          {

                   My.Trace.Write(Request.QueryString["Doesn'tExist"].ToString());

          }

}

 

This is all nice and neat and encapsulated now, and gives you a performance increase which scales with your application through debug and in to release, as well as error handling of your Trace commands, all this with the ease of use of the Trace methods them selves.


View Entire Article

User Comments

Title: ateeq   
Name: helo
Date: 2012-12-18 4:43:34 AM
Comment:
flksd fsdjlk fjds djs sda s sadlj falk asldfj as
Title: Programmer   
Name: Terence
Date: 2004-07-16 12:42:52 PM
Comment:
It looks like the last post cut some of the code off... but you get the idea. Terence.
Title: Programmer   
Name: Terence
Date: 2004-07-16 12:40:45 PM
Comment:
Nice job Phil. I tranlated your C# code to vb.net for those who are interested in that. Cheers. Terence
'Translated from C# to VB.net.
'July 16, 2004
'Terence J Wehle
'We use shared Methods in this translation so that creating an instance
'of the object is not necessary. This is appropriate since
'each methods is acting only on passed in parameters.

'In this article, Phil Winstanley proves that contrary to the
'NET SDK documentation that “Trace statements are processed and displayed
'only when tracing is enabled.”, the statements are in fact processed,
'although not displayed. If you have many such statements, particularly
'in a loop, this could slow things down for the page even thought the
'Trace is set to "False". This solution checks first whether tracing
'is enable before making the call and thus save resources. It is a
'wrapper for the trace calls.
'---------------------
Imports System
Namespace TJW


Public Class Trace

Public Sub Write(ByVal Message As String)

If (System.Web.HttpContext.Current.Trace.IsEnabled) Then

Try
System.Web.HttpContext.Current.Trace.Write(Message.ToString())
Catch e As Exception
System.Web.HttpContext.Current.Trace.Warn("Trace", "Write", e)
End Try

End If

End Sub

Shared Sub Write(ByVal Category As String, ByVal Message As String)

If (System.Web.HttpContext.Current.Trace.IsEnabled) Then

Try
Throw New Exception("This is a bogus error")
System.Web.HttpContext.Current.Trace.Write(Category.ToString(), Message.ToString())
Catch e As Exception
System.Web.HttpContext.Current.Trace.Warn("Trace", "Write", e)
End Try

End If

End Sub

Public Sub Write(ByVal Category As String, ByVal Message As Stri






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-09-17 11:11:26 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search