2009-07-24 53 views
0

我在web.config中啓用了trace pageoutput =「true」,我喜歡它提供的在頁面底部看到所有這些東西的簡單方法。如何將追蹤輸出添加到httphandler的context.response中?

我想從我的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; 
     } 
    } 
} 

我特別想看到的窗體和查詢字符串的集合,但是這一切給人的「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跟蹤查詢字符串的集合做任何其他的想法。

回答

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

Reflectoring超過System.Web.TraceContext.EndRequest應該給你足夠的創建自己的TracingHttpHandler如果你不能使用反射。

編輯: 看起來你忘了BindingFlags.Instance。此外,我猜你已將using (var htw = ...)更改爲using (object htw = ...),這會給你「類型必須隱式轉換爲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 }); 
     } 
    } 
} 
+0

想你使用塊,他抱怨不得不實現IDisposable所以我嘗試: 公共無效的ProcessRequest(HttpContext的背景下) { context.Response.ContentType = 「text/plain的」; context.Response.Write(「Hello World」); object htw = new System.Web.UI.Html32TextWriter(context.Response。輸出); { typeof(TraceContext) .GetMethod(「Render」,System.Reflection.BindingFlags.NonPublic) .Invoke(HttpContext.Current.Trace,new object [] {htw}); } } Err on typeof – 2009-07-24 21:55:56

0

我不認爲ASP.NET跟蹤工作,除了頁面。

輸出是否在頁面的底部有關係嗎?如果輸出是在單獨的文件或事件日誌中怎麼辦?你可以用ASP.NET Health Monitoring來做這件事。

0

其實這是很簡單的,只要你有正確的異常類型擺脫ASP.Net此信息。我在我的global.asax錯誤處理程序中使用這個MVC應用程序來給我發送死亡堆棧跟蹤的黃色屏幕,我通常不會看到;第二條語句組

 // 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被拋出(),其中有適當的方法和黏糊糊的收穫,並吐出所有的好東西。