HTTP Module的EndRequest没有执行

li_tttt 2010-11-24 02:48:26
因站点页面有时会出现一下子卡住的问题,为了监测访问,给网站加了一个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());
}

}
...全文
49 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
li_tttt 2010-11-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 newdigitime 的回复:]

某些代码中是否有
Response.Redirect("xxx.aspx",false);
[/Quote]

我试过了,依然会有Application_EndRequest事件,从帮助看到,即使在Application_BeginRequest开始的时候调用RequestComplete,也会有Application_EndRequest事件,这是否是.net底层框架出了问题?
newdigitime 2010-11-24
  • 打赏
  • 举报
回复
某些代码中是否有
Response.Redirect("xxx.aspx",false);

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧