HTTP Module的EndRequest没有执行

li_tttt li_tttt 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
因站点页面有时会出现一下子卡住的问题,为了监测访问,给网站加了一个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());
}

}
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
li_tttt
.NET社区 62,275 成员 668,968 内容