我問過關於創建IIS日誌記錄器的一個問題,但我仍然有幾個問題:IHttpModule的問題
- 原始郵件丟失
- 響應消息沒有捕捉到
完全可以將這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 提前感謝!
你* *嚴重WO左右在多線程環境中使用單個物理文件*這種方式*? – 2012-01-01 23:20:18
請務必在您的其他問題中標記您的答案,以便將來的讀者可以從您的經驗中學習。 – 2012-01-01 23:31:37
爲了簡單起見,我只是把它放在那裏。我的問題與多線程無關,這就是爲什麼我將它排除在外的原因。無論如何,我確實將答案標記爲有用,但由於某種原因,它沒有反映在頁面上。我確實在評論部分留下了我的發現以備將來參考。 – Raytrace 2012-01-02 06:42:49