2012-01-01 6 views
-1

私はIISロガーを作成に関する質問をしてきましたが、それでも問題のカップルを持っています:のIHttpModule問題

  1. オリジナルメッセージが
  2. 応答メッセージが

をキャプチャされませんが失われますこれらの2つを整理することは可能なのでしょうか?

IHTTPハンドラ:

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

多量のマルチスレッド環境で単一の物理ファイルを使用する*この方法*? –

+0

将来の読者があなたの経験から学ぶことができるように、あなたの他の質問に答えを記入してください。 –

+0

私は単純にするためにそこに置いています。私の質問はマルチスレッドに関連していないので、私はそれを断念しました。いずれにせよ、私は答えを有用とマークしましたが、何らかの理由でそれがページに反映されませんでした。そして私は、将来の参考のためにコメント欄に私の発見を残しました。 – Raytrace

答えて

1

HTTPHandlerのポイントはわかりませんが、すべてのログはHTTPModuleから実行できます。しかし、あなたのコードは生き残るためにはかなりの改善が必要です。

1)特に邪魔にならないようにしようとしている場合は、未処理の例外がスローされないように、ストリームライターの周りにtry/catchブロックを置く必要があります。

2)ストリームライターコードは、リソースをストリングしないように、使用ブロックでラップする必要があります。

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 7+ここ

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

が書き換えられたコードです)統合:あなたは、WOについて*深刻*は

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

関連する問題