当前位置:编程学习 > C#/ASP.NET >>

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

        } --------------------编程问答-------------------- 某些代码中是否有
Response.Redirect("xxx.aspx",false); --------------------编程问答--------------------
引用 1 楼 newdigitime 的回复:
某些代码中是否有
Response.Redirect("xxx.aspx",false);


我试过了,依然会有Application_EndRequest事件,从帮助看到,即使在Application_BeginRequest开始的时候调用RequestComplete,也会有Application_EndRequest事件,这是否是.net底层框架出了问题?
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,