【发布时间】:2012-01-01 23:10:13
【问题描述】:
我问过一个关于创建 IIS 记录器的问题,但仍有几个问题:
- 原始消息丢失
- 未捕获响应消息
有没有可能解决这两个问题?
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 提前致谢!
【问题讨论】:
-
您是否认真在高度多线程的环境中以这种方式处理单个物理文件?
-
请务必在您的其他问题中标记您的答案,以便未来的读者可以从您的经验中学习。
-
为了简单起见,我只是把它放在那里。我的问题与多线程无关,这就是我忽略它的原因。无论如何,我确实将答案标记为有用,但由于某种原因它没有反映在页面上。我确实将我的发现留在了 cmets 部分以供将来参考。
标签: c# asp.net logging httphandler httpmodule