I have a working solution with two caveats:
-, , Page.ProcessRequest() ( Response ), Render, , ( EndRender).
, , , , . . , ( , ).
-, -, , HttpRuntime.Profile, internal System.Web, , , private Page. , , , . ASP.NET , SOL.
:
using System;
using System.Reflection;
using System.Web;
using System.Web.UI;
namespace StackOverflow.Bounties.Web.UI
{
public class TraceablePage : Page
{
public bool EnableTraceOutput
{
get;
set;
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
if (!EnableTraceOutput) {
return;
}
BindingFlags evilFlags
= BindingFlags.Instance | BindingFlags.Static
| BindingFlags.Public | BindingFlags.NonPublic;
object profiler = typeof(HttpRuntime)
.GetProperty("Profile", evilFlags).GetGetMethod(true)
.Invoke(null, null);
profiler.GetType().GetProperty("PageOutput", evilFlags)
.GetSetMethod(true).Invoke(profiler, new object[] { true });
typeof(Page).GetMethod("ProcessRequestEndTrace", evilFlags)
.Invoke(this, null);
profiler.GetType().GetProperty("PageOutput", evilFlags)
.GetSetMethod(true).Invoke(profiler, new object[] { false });
}
}
}
, AutoPostBack, :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestTracePage.aspx.cs"
Inherits="StackOverflow.Bounties.Web.UI.TestTracePage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>TraceablePage Test</title>
</head>
<body>
<form id="form" runat="server">
<h2>TraceablePage Test</h2>
<p>
<asp:CheckBox id="enableTrace" runat="server"
AutoPostBack="True" Text="Enable trace output"
OnCheckedChanged="enableTrace_CheckedChanged" />
</p>
</form>
</body>
</html>
:
using System;
using System.Web.UI;
namespace StackOverflow.Bounties.Web.UI
{
public partial class TestTracePage : TraceablePage
{
protected void enableTrace_CheckedChanged(object sender, EventArgs e)
{
EnableTraceOutput = enableTrace.Checked;
}
}
}
:

:

, .