0

我在 web.config 中启用了 trace pageoutput="true",我喜欢它提供的在页面底部查看所有这些内容的简单方法。

我想从我的 httphandler 输出底部的跟踪中获得相同的输出。有没有办法通过遵循此代码的代码转储相同的跟踪信息:

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;
        }
    }
}

我特别想看看 Forms 和 QueryString 集合,但所有这些都是“Hello World”。

-- 编辑更新 2009 年 7 月 25 日:

    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;
        }
    }
}

对于如何最轻松地获得表单和查询字符串集合的格式化转储(如 pageOutput 跟踪),我也愿意接受任何其他想法。

4

3 回答 3

1

您可以从HttpContext.Current.Trace.TraceFinished获取您自己的跟踪事件。不幸的是,页面跟踪(包括 Forms、QueryString 等的所有优点)被锁定在内部方法中。

如果你对反射没意见,你可以这样称呼它:

using (var htw = new System.Web.UI.Html32TextWriter(response.Output)) {
    typeof(TraceContext)
        .GetMethod("Render",BindingFlags.NonPublic | BindingFlags.Instance)
        .Invoke(HttpContext.Current.Trace, new object[] { htw });
}

如果你不能使用反射,反射System.Web.TraceContext.EndRequest应该给你足够的空间来创建你自己的。TracingHttpHandler

编辑:看起来你忘记了BindingFlags.Instance。另外,我猜您更改using (var htw = ...)using (object htw = ...)which 会给您“类型必须可隐式转换为 IDisposable”错误。如果你不能使用var,那么你将不得不把它写成using (Html32TextWriter htw = ...).

完整样本:

<%@ WebHandler Language="C#" Class="UploadHandler" %>

using System;
using System.Web;
using System.Web.UI;
using System.Reflection;

public class UploadHandler : IHttpHandler {
    public bool IsReusable { 
       get { return true; }
    }

    public void ProcessRequest(HttpContext context) {
       // the output will suck as text/plain - Render outputs HTML.
       context.Response.ContentType = "text/html"; 
       context.Response.Write("Hello World!");

       // depending on web.config settings, you may need to enable tracing manually
       HttpContext.Current.Trace.IsEnabled = true;
       // I had to write a custom trace message, or the Request context wasn't captured - YMMV
       HttpContext.Current.Trace.Write(null);

       using (Html32TextWriter htw = new Html32TextWriter(context.Response.Output)) {
          typeof(TraceContext)
              .GetMethod("Render", BindingFlags.NonPublic | BindingFlags.Instance)
             .Invoke(HttpContext.Current.Trace, new object[] { htw });
       } 
    }
}
于 2009-07-24T20:56:54.860 回答
0

我认为除了页面之外,ASP.NET 跟踪不起作用。

输出是否在页面底部有关系吗?如果输出位于单独的文件或事件日志中怎么办?您可能可以使用ASP.NET Health Monitoring来做到这一点。

于 2009-07-24T20:23:45.037 回答
0

实际上,只要您有正确的异常类型,从 ASP.Net 获取此信息非常简单。我在 MVC 应用程序的 global.asax 错误处理程序中使用它来向我发送我通常不会看到的死亡堆栈跟踪的黄屏;

        // See if ASP.Net has some error handling info for us first
        string htmlError = null;
        var httpException = e as HttpException;
        if (httpException != null)
            htmlError = httpException.GetHtmlErrorMessage();
        else
        {
            // Ok, we'll generate our own then
            var h = new HttpUnhandledException(e.Message, e);
            htmlError = h.GetHtmlErrorMessage();
        }

请注意,在第二个语句组中,我将抛出的任何异常简单地包装在 HttpUnhandledException() 中,该异常具有适当的方法和垃圾来收集和吐出所有好​​东西。

于 2012-07-05T09:15:46.403 回答