I’ve enabled trace pageoutput=”true” in my web.config and I like the easy way it provides of seeing all this stuff at the bottom of the page.
I’d like to get the same output from trace at the bottom of the output from my httphandler. Is there a way to dump out the same trace info via code that would follow this code:
public class UploadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
}
public bool IsReusable
{
get
{
return false;
}
}
}
I particularly want to see the Forms and QueryString collections but all this gives is “Hello World”.
— edit update 7/25/2009:
public class UploadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
object htw = new System.Web.UI.Html32TextWriter(context.Response.Output);
{
typeof(TraceContext)
.GetMethod("Render", System.Reflection.BindingFlags.NonPublic)
.Invoke(HttpContext.Current.Trace, new object[] { htw });
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
I am also open to any other ideas about how to most easily get a formatted dump of the forms and querystring collections like pageOutput trace does.
You can get your own trace events from HttpContext.Current.Trace.TraceFinished. Unfortunately, the page tracing (which includes all the goodies of Forms, QueryString, etc.) is locked away in internal methods.
If you’re okay with reflection, you can call it like:
Reflectoring over
System.Web.TraceContext.EndRequestshould give you enough to create your ownTracingHttpHandlerif you can’t use reflection.Edit:
It looks like you forgot
BindingFlags.Instance. Also, I’m guessing you changedusing (var htw = ...)tousing (object htw = ...)which would give you the “type must be implicitly convertible to IDisposable” error. If you can’t usevar, then you’ll have to write it asusing (Html32TextWriter htw = ...).Full sample: