2009-07-24 3 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; 
     } 
    } 
} 

나는 특히 양식 및 쿼리 스트링 컬렉션을보고 싶어하지만,이 모든 것이 제공이 "안녕하세요"입니다 :이 코드를 따를 것입니다 코드를 통해 같은 추적 정보를 덤프하는 방법입니다.

- 편집 업데이트 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을 구현하는 데 대해 불평 그래서 시도 : 공공 무효이 ProcessRequest (HttpContext를 컨텍스트) { context.Response.ContentType = "텍스트/일반"; context.Response.Write ("Hello World"); 개체 htw = 새 System.Web.UI.Html32TextWriter (context.Response.산출); { typeof (TraceContext) .GetMethod ("Render", System.Reflection.BindingFlags.NonPublic) .Invoke (HttpContext.Current.Trace, new object [] {htw}); } } 대해서 ​​typeof –

+0

@ 존 갈트에 ERR - 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

안녕하세요. Mark ... 감사합니다 ... 위의 수정 사항 업데이트를 참조하십시오. –

0

페이지를 제외하고 ASP.NET 추적이 작동하지 않는다고 생각합니다.

출력이 페이지 하단에 있다면 중요합니까? 출력이 개별 파일이나 이벤트 로그에 있다면 어떻게 될까요? ASP.NET Health Monitoring으로 thsi를 할 수 있습니다.

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

공지 사항이 수확 모든 케이크를 뱉어하는 적절한 방법과 빚이있다 (A HttpUnhandledException에서 발생 된 어떤 예외 I 단순 포장).

관련 문제