Accessing ASP.NET Trace Data


A few people have asked me if it is possible to programmatically access the data from
the ASP.NET Trace. While this is certainly not supported, if you have the correct
permissions you can use reflection to grab the DataSet that holds all of this data.
See the following code snippets for an example of grabbing the DataSet that is being
filled by the current request and for an example of getting the ArrayList that stores
all DataSets for previous requests (used by the trace HttpHandler).

/// <summary>Gets
the data for the currently in-process trace.<
/summary>

/// <returns>The
data for the currently in-process trace.<
/returns>

private static DataSet GetCurrentTraceDataSet()

{
 

       BindingFlags flags
=

              BindingFlags.Instance | BindingFlags.Static |

              BindingFlags.Public | BindingFlags.NonPublic;





       //
Get the DataSet

       MethodInfo getData = typeof(TraceContext).GetMethod(“GetData“,
flags);

       return (DataSet)getData.Invoke(HttpContext.Current.Trace, null);

}

 



/// <summary>Gets
a list of all of the DataSets for previously traced requests.<
/summary>

/// <returns>A
list of all of the DataSets for previously traced requests.<
/returns>

private static ArrayList GetTraceLogDataSets()

{

       BindingFlags flags
=

              BindingFlags.Instance | BindingFlags.Static |

              BindingFlags.Public | BindingFlags.NonPublic;



       //
Get the profiler

       PropertyInfo piProfile = typeof(HttpRuntime).GetProperty(“Profile”,
flags);

       MethodInfo getProfiler = piProfile.GetGetMethod(true);

       object profiler
= getProfiler.Invoke(null, null);



       //
Gets the list of all datasets containing the trace data

       MethodInfo getData = profiler.GetType().GetMethod(“GetData“,
flags);

       return (ArrayList)getData.Invoke(profiler, null);

}

With this data you can now write your own mining tools, your own UI, etc. Yeah reflection!!

Comments (3)

  1. Fluxtah says:

    Cheers, I was afraid it would be something like this, it would be nice if this was made public in the future!

  2. toub says:

    In ASP.NET 2.0, you can subscribe to the TraceContext.TraceFinished event.

  3. Randhir says:

    Error for this function…Unable to cast object of type ‘System.Object[]’ to type ‘System.Collections.ArrayList’.

    Please give some idea…..

    private static ArrayList GetTraceLogDataSets()

    {

          BindingFlags flags =

                 BindingFlags.Instance | BindingFlags.Static |

                 BindingFlags.Public | BindingFlags.NonPublic;

          // Get the profiler

          PropertyInfo piProfile = typeof(HttpRuntime).GetProperty("Profile", flags);

          MethodInfo getProfiler = piProfile.GetGetMethod(true);

          object profiler = getProfiler.Invoke(null, null);

          // Gets the list of all datasets containing the trace data

          MethodInfo getData = profiler.GetType().GetMethod("GetData", flags);

          return (ArrayList)getData.Invoke(profiler, null);

    }

    With