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());
}
}