HTTP Module的EndRequest没有执行
因站点页面有时会出现一下子卡住的问题,为了监测访问,给网站加了一个HttpModule,分别在开始请求和结束请求时记下日志,但发现在实际环境中,会有部份只有开始,没有结束的日志,请问在什么情况下会有不执行EndRequest事件?
(注:还有一个用于认证的HttpModule在这个Module前运行)
httpModule代码如下:
private ILog _log;
private DateTime d1 = new DateTime(2000, 1, 1);
public void Init(HttpApplication context)
{
//用AuthenticateRequest 是为了获取一些其它信息,应该不会影响EndRequest事件
context.AuthenticateRequest += new EventHandler(Application_BeginRequest);
context.EndRequest += new EventHandler(Application_EndRequest);
}
private void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
HttpRequest request = application.Request;
HttpResponse response = application.Response;
try
{
XmlConfigurator.Configure();
_log = LogManager.GetLogger(ConfigurationManager.AppSettings["loggerName"]);
bool needCheck = IsCheck(request.RawUrl);
if (needCheck)
{
string gid = Guid.NewGuid().ToString();
//string id = context.Items["____userid"].ToString();
context.Items["test_uid"] = gid;
TimeSpan ts=DateTime.Now-d1;
context.Items["test_starttime"] = ts.TotalMilliseconds;
_log.Info("gid:" + gid + ",开始请求url:" + request.RawUrl);
}
}
catch (Exception ex)
{
_log.Error(ex.ToString());
}
}
private void Application_EndRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
HttpRequest request = application.Request;
HttpResponse response = application.Response;
try
{
bool needCheck = IsCheck(request.RawUrl);
if (needCheck)
{
string id = context.Items["test_uid"].ToString();
double interval = 0;
try
{
TimeSpan ts = DateTime.Now - d1;
interval =ts.TotalMilliseconds- Convert.ToDouble(context.Items["test_starttime"]);
}
catch
{
}
_log.Info("gid:" + id + ",结束请求url:" + request.RawUrl + ",耗时(毫秒):" + interval.ToString());
}
}
catch (Exception ex)
{
_log.Error(ex.ToString());
}
}
(注:还有一个用于认证的HttpModule在这个Module前运行)
httpModule代码如下:
private ILog _log;
private DateTime d1 = new DateTime(2000, 1, 1);
public void Init(HttpApplication context)
{
//用AuthenticateRequest 是为了获取一些其它信息,应该不会影响EndRequest事件
context.AuthenticateRequest += new EventHandler(Application_BeginRequest);
context.EndRequest += new EventHandler(Application_EndRequest);
}
private void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
HttpRequest request = application.Request;
HttpResponse response = application.Response;
try
{
XmlConfigurator.Configure();
_log = LogManager.GetLogger(ConfigurationManager.AppSettings["loggerName"]);
bool needCheck = IsCheck(request.RawUrl);
if (needCheck)
{
string gid = Guid.NewGuid().ToString();
//string id = context.Items["____userid"].ToString();
context.Items["test_uid"] = gid;
TimeSpan ts=DateTime.Now-d1;
context.Items["test_starttime"] = ts.TotalMilliseconds;
_log.Info("gid:" + gid + ",开始请求url:" + request.RawUrl);
}
}
catch (Exception ex)
{
_log.Error(ex.ToString());
}
}
private void Application_EndRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
HttpRequest request = application.Request;
HttpResponse response = application.Response;
try
{
bool needCheck = IsCheck(request.RawUrl);
if (needCheck)
{
string id = context.Items["test_uid"].ToString();
double interval = 0;
try
{
TimeSpan ts = DateTime.Now - d1;
interval =ts.TotalMilliseconds- Convert.ToDouble(context.Items["test_starttime"]);
}
catch
{
}
_log.Info("gid:" + id + ",结束请求url:" + request.RawUrl + ",耗时(毫秒):" + interval.ToString());
}
}
catch (Exception ex)
{
_log.Error(ex.ToString());
}
}
...全文
相关推荐
ASP.Net中自定义Http处理及其应用
ASP.Net中自定义Http处理及其应用
asp.net中实现类似AOP功能
HttpHandler实现了类似于ISAPI Extention的功能,他处理请求(Request)的信息和发送响应(Response)。HttpHandler功能的实现通过实现IHttpHandler接口来达到。而HttpModule实现了类似于ISAPI Filter的功能。
在ASP.NET中实现Url Rewriting (二)
概要 分析如何使用微软提供的ASP.NET来对动态产生的URL地址进行网址重写。 网址重写是实现一种截取网址请求并将其进行处理后重新指向到一个指定的网址的过程。作者本人在对各种实现网址重写的技术进行研究和探讨后得出的经验和方法,希望能对您有所帮助。
HTTPModules入门篇[借鉴].pdf
HTTPModules入门篇[借鉴].pdf
.net生命周期详细介绍
这是。net面试最容易问到的问题,看过这篇文档,你就会懂了的


