代码之家  ›  专栏  ›  技术社区  ›  Omnia9

重定向trace.axd输出

  •  5
  • Omnia9  · 技术社区  · 15 年前

    我有一个自定义控件,它只显示一组给定的配置值。

    我要捕获trace.axd数据并将其输出到此控件。

    Web.CONFIG

    writeToDiagnosticsTrace="true" 
    ...
    <listeners>
     name="WebPageTraceListener"
        type="System.Web.WebPageTraceListener, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
    </listeners>
    

    我希望能够在用户控件中加载trace.axd文件。然后在需要时加载该用户控件。

    1 回复  |  直到 15 年前
        1
  •  3
  •   Frédéric Hamidi    15 年前

    我有一个有效的解决方案,有两个注意事项:

    首先,它总是过早地呈现跟踪输出,因为在a page.processrequest() override(the response object has already been cleaned up)中这样做太迟了,所以我们被迫执行它在 render phase期间,这意味着我们将错过一些消息(最明显的是 endrender )。

    在控件中实现该行为会加剧问题,因为我们必须确保控件是页面上最后呈现的内容,以避免丢失更多消息。出于这个原因,我选择实现自定义页面类而不是自定义控件类。如果你绝对需要一个控制类,它应该很容易转换(但如果你需要帮助,请在这里留下一个字)。

    第二,拥有数据的探查器对象, httpruntime.profile ,is internal to the system.web assembly,and course the trace rendering routine is private to the page class.所以我们必须滥用反射,打破封装,并且基本上是 邪恶的 来做你想要做的。如果ASP.NET跟踪实现在最短时间内发生更改,我们就是sol。

    也就是说,这里是可跟踪页面类:

    使用系统;
    使用系统。反射;
    使用system.web;
    使用system.web.ui;
    
    命名空间stackoverflow.boundies.web.ui
    {
    公共类可跟踪page:page
    {
    ///<摘要>
    ///获取或设置是否呈现跟踪输出。
    ///</summary>
    公共bool启用跟踪输出
    {
    得到;
    集合;
    }
    
    ///<摘要>
    ///滥用反射来强制探查器的页面输出标志
    ///在调用页的跟踪呈现例程期间为true。
    ///</summary>
    受保护的重写void呈现(htmlTextWriter编写器)
    {
    base.render(作者);
    如果(!)启用跟踪输出){
    返回;
    }
    
    //允许访问私有和内部成员。
    绑定标记evilflags
    =bindingFlags.instance bindingFlags.static
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    Page.ProcessRequest()ResponseRenderEndRender

    HttpRuntime.ProfileinternalSystem.WebprivatePage

    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's page output flag
            /// to true during a call to the page's 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