2009-07-24 14 views
0

私はweb.configのトレース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; 
     } 
    } 
} 

私は特にフォームとの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トレースがないように、最も簡単な形態とクエリ文字列コレクションのフォーマットされたダンプを取得する方法についての他のアイデアを開いています。

答えて

1

HttpContext.Current.Trace.TraceFinishedから独自のトレースイベントを取得できます。残念ながら、ページトレース(Forms、QueryStringなどのすべての機能が含まれています)は内部メソッドでロックされています。あなたが反射して大丈夫なら

することは、あなたが好きそれを呼び出すことができます。System.Web.TraceContext.EndRequest上Reflectoring

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

あなたはリフレクションを使用できない場合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インターを実装する必要が不満ので、私が試した: ます。public voidのprocessRequest(のHttpContextコンテキスト) { context.Response.ContentType = "text/plainの"; context.Response.Write( "Hello World"); オブジェクトhtw = new System.Web.UI.Html32TextWriter(context.Response。出力); { typeof(TraceContext) .GetMethod( "Render"、System.Reflection.BindingFlags.NonPublic) .Invoke(HttpContext.Current.Trace、new object [] {htw}); } } エラーが発生しました –

+0

@John Galt - Html32TextWriter *はIDisposable(http://msdn.microsoft.com/en-us/library/system.web.ui.html32textwriter.dispose.aspx)とtypeof標準のC#(http://msdn.microsoft.com/en-us/library/58918ffs(VS.71).aspx)です。すべてのコードをあなたの質問に投稿して(書式設定されているので)、構文をチェックすることができます。 –

+0

こんにちはマーク...ありがとうございました。上記の編集更新をご覧ください。 –

0

ページを除いてASP.NETトレースが機能しているとは思われません。

出力がページの下部にあるかどうかは重要ですか?出力が別のファイルにある場合、またはイベントログに含まれる場合はどうなりますか?あなたはおそらくASP.NET Health Monitoringでthsiをすることができます。

0

は、実際にそれは限り、あなたは正しい例外タイプを持っているとして、ASP.Netからこの情報を取得するために非常に簡単です。私は私のglobal.asaxエラーハンドラでこれを使用して、MVCアプリケーションのために、私が通常見ることのできないデススタックトレースの黄色の画面を電子メールで送ります。すべてのグッズを収穫し、吐き出すための適切な方法とネバネバしたものを有する第2文グループ例外がHttpUnhandledExceptionにスローされているものは何でも私の単純なラップ()内

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

注意してください。

関連する問題