Trace.axd output redirection

I have a custom control that only displays with a given set of configuration values.

I want to capture 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 load the trace.axd file in usercontrol. Then load this usercontrol if necessary.

+5
source share
1 answer

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
    {
        /// <summary>
        /// Gets or sets whether to render trace output.
        /// </summary>
        public bool EnableTraceOutput
        {
            get;
            set;
        }

        /// <summary>
        /// Abuses reflection to force the profiler page output flag
        /// to true during a call to the page trace rendering routine.
        /// </summary>
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            if (!EnableTraceOutput) {
                return;
            }

            // Allow access to private and internal members.
            BindingFlags evilFlags
                = BindingFlags.Instance | BindingFlags.Static
                | BindingFlags.Public | BindingFlags.NonPublic;

            // Profiler profiler = HttpRuntime.Profile;
            object profiler = typeof(HttpRuntime)
                .GetProperty("Profile", evilFlags).GetGetMethod(true)
                .Invoke(null, null);

            // profiler.PageOutput = true;
            profiler.GetType().GetProperty("PageOutput", evilFlags)
                .GetSetMethod(true).Invoke(profiler, new object[] { true });

            // this.ProcessRequestEndTrace();
            typeof(Page).GetMethod("ProcessRequestEndTrace", evilFlags)
                .Invoke(this, null);

            // profiler.PageOutput = false;
            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;
        }
    }
}

:

Trace disabled

:

Trace enabled

, .

+3

All Articles