I have a custom control that is show only with a given set of config values.
I want to capture the trace.axd data and output it to this control.
web.config
writeToDiagnosticsTrace="true"
...
<listeners>
name="WebPageTraceListener"
type="System.Web.WebPageTraceListener, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
</listeners>
I want to be able to load the trace.axd file in a usercontrol. Then have that usercontrol be loaded whenever needed.
I have a working solution, with two caveats:
First, it will always render the trace output too early, because it’s too late to do that in a Page.ProcessRequest() override (the
Responseobject has already been cleaned up), so we’re forced to do it during theRenderphase, which means we’ll miss some messages (most notablyEndRender).Implementing that behavior in a control exacerbates the problem, since we’d have to ensure our control is the last thing to render on the page in order to avoid missing more messages. For that reason, I chose to implement a custom page class instead of a custom control class. If you absolutely need a control class, it should be easy to convert (but leave me a word here if you need help).
Second, the profiler object that owns the data,
HttpRuntime.Profile, isinternalto theSystem.Webassembly, and of course the trace rendering routine isprivateto thePageclass. So we have to abuse reflection, break encapsulation, and basically be evil in order to do what you want. If the ASP.NET trace implementation changes in the slightest, we’re SOL.That said, here’s the traceable page class:
And here’s its test page, which uses an
AutoPostBackcheck box to demonstrate its behavior across postbacks:And the code behind:
The test page renders like this on first load:
Checking the box posts back and renders the trace output:
Clearing the check box again suppresses the trace output, as expected.