2012-01-01 1 views
-1

가 나는 IIS 로거를 생성에 관한 질문을했지만, 여전히 문제의 몇 데 :IHttpModule 문제

  1. 원래 메시지가
  2. 응답 메시지가

캡처되지 않은 손실을 이 2 가지를 정리하는 것이 가능한가?

을 IHttpHandler :

using System.Web; 
using System.IO; 

namespace MyLogger 
{ 
    public class MyHandler : IHttpHandler 
    { 
     public void ProcessRequest(HttpContext context) 
     { 
      context.Response.Write("The page request is " + context.Request.RawUrl.ToString()); 
      StreamWriter sw = new StreamWriter(@"C:\requestLog.txt", true); 
      sw.WriteLine("Page requested at " + DateTime.Now.ToString() + context.Request.RawUrl); 
      sw.Close(); 
     } 

     public bool IsReusable 
     { 
      get 
      { 
       return true; 
      } 
     } 
    } 
} 

IHttpModule :

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Web; 
using System.IO; 

namespace MyLogger 
{ 
    public class MyModule : IHttpModule 
    { 
     public InterceptorModule() 
     { } 
     public void Init(HttpApplication objApplication) 
     { 
      // Register event handler of the pipe line 
      objApplication.BeginRequest += new EventHandler(this.ContextBeginRequest); 
      objApplication.EndRequest += new EventHandler(this.ContextEndRequest); 
     } 
     public void Dispose() 
     { 
     } 
     public void ContextEndRequest(object sender, EventArgs e) 
     { 
      StreamWriter sw = new StreamWriter(@"C:\requestLog.txt", true); 
      sw.WriteLine("End Request called at " + DateTime.Now.ToString()); sw.Close(); 
     } 
     public void ContextBeginRequest(object sender, EventArgs e) 
     { 
      StreamWriter sw = new StreamWriter(@"C:\requestLog.txt", true); 
      sw.WriteLine("Begin request called at " + DateTime.Now.ToString()); sw.Close(); 
     } 
    } 
} 

내 이전 게시물 : 사전에 IIS API Monitor in a web application 감사합니다!

+2

당신이 심각한 * 있습니까 * WO에 대한 : 7+

여기
<configuration> <system.webServer> <modules> <add name="MyModule" type="MyNamespace.MyModule"/> </modules> </system.webServer> </configuration> 

는 다시 코드) 통합 과도하게 멀티 쓰레딩 된 환경에서 하나의 실제 파일을 사용하여 rking * 할 수 있습니다. –

+0

미래의 독자가 귀하의 경험을 통해 배울 수 있도록 귀하의 다른 질문에 답을 표시하십시오. –

+0

나는 단순함을 위해서 거기에 올려 놓을 뿐이다. 내 질문은 멀티 스레딩과 관련이 없으므로이를 생략했습니다. 어쨌든 나는 대답을 유용하게 표시했는데 어떤 이유로 그것이 페이지에 반영되지 않았다. 그리고 나는 나중에 참조 할 수 있도록 코멘트 섹션에 내 결과를 남겼습니다. – Raytrace

답변

1

HTTPHandler의 요점은 무엇인지 모르겠지만 모든 로깅은 HTTPModule에서 수행 할 수 있습니다. 그러나 코드는 생존하기 위해 상당한 개선이 필요합니다.

1) 특히 눈에 거슬리지 않으려는 경우 처리되지 않은 예외가 throw되지 않도록 스트림 작성자 주변에서 블록을 시도하거나 catch해야합니다.

2) streamwriter 코드는 리소스를 줄이지 않도록하기 위해 using 블록으로 묶어야합니다.

3) 잠재적으로 파일에 동시에 쓰려고하는 여러 스레드가있을 수 있으므로 쓰기 블록을 잠금 블록으로 래핑해야합니다.

4) HttpContext.Current.Request를 사용하여 현재 요청에 액세스 할 수 있습니다.이 요청은 HttpModule에서 수행 한 것일 수 있습니다. 이것이 의도가 아니라면 추가 설명이 필요합니다.

5) 디버그 모드에서 응용 프로그램을 시작하고 Init 메서드가 실행되지 않으면 web.config 항목이 올바르지 않습니다. 유형은 완전해야합니다 (즉, 네임 스페이스를 포함)하고 통합 클래스 모드 구성을 모두 추가해야합니다 :

클래식 모드 (IIS 6, IIS 7 + 클래식)

<configuration> 
    <system.web> 
    <httpModules> 
     <add name="MyModule" type="MyNamespace.MyModule"/> 
    </httpModules> 
    </system.web> 
</configuration> 

통합 모드 (IIS

static Object m_LockObject = new Object(); 

    public void Init(HttpApplication objApplication) 
    { 
     // Register event handler of the pipe line 
     objApplication.BeginRequest += new EventHandler(this.ContextBeginRequest); 
     objApplication.EndRequest += new EventHandler(this.ContextEndRequest); 
    } 
    public void ContextEndRequest(object sender, EventArgs e) 
    { 
     try 
     { 
      lock (m_LockObject) 
      { 
       using (StreamWriter sw = new StreamWriter(@"C:\requestLog.txt", true)) 
       { 
        sw.WriteLine("End request called at " + DateTime.Now.ToString() + "; URL: " + HttpContext.Current.Request.RawUrl.ToString()); 
       } 
      } 

      // Write the response back to the caller 
      HttpContext.Current.Response.Write("The page request is " + HttpContext.Current.Request.RawUrl.ToString()); 

     } 
     catch 
     { 
     } 
    } 
    public void ContextBeginRequest(object sender, EventArgs e) 
    { 
     try 
     { 
      lock (m_LockObject) 
      { 
       using (StreamWriter sw = new StreamWriter(@"C:\requestLog.txt", true)) 
       { 
        sw.WriteLine("Begin request called at " + DateTime.Now.ToString() + "; URL: " + HttpContext.Current.Request.RawUrl.ToString()); 
       } 
      } 
     } 
     catch 
     { 
     } 
    } 
+0

답장을 보내 주셔서 감사합니다. 그러나 원래 메시지를 로깅하여 호출 클라이언트에 전달해야 할 필요는 없습니다. HttpModule 및/또는 HttpHandler를 사용하여이 작업을 수행 할 수 있습니까? – Raytrace

+0

@Raytrace : 예, 이것은 'HttpContext.Current.Response.Write'를 사용하여 절대 수행 할 수 있습니다.원래 http 모듈에서 미안 해요. 나는 이것이 'ContextEndRequest'에서 완료되도록 답변을 업데이트 했으므로 페이지의 맨 아래에 나타납니다. –

+0

나중에 참조 할 수 있도록 내 대답을 찾았습니다. http://forums.asp.net/post/2417324.aspx – Raytrace