我已经提出了有关创建IIS记录器的问题,但我仍然遇到了一些问题:
是否可以将这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 提前谢谢!
答案 0 :(得分:1)
我不确定HTTPHandler的意义,但所有日志记录都可以从HTTPModule执行。但是,为了生存,您的代码需要一些实质性的改进。
1)你应该在streamwriters周围尝试/ 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>
这是重写的代码:
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
{
}
}