使用Application_Error捕获站点错误并写日志

Global.ascx页面使用以下方法即可捕获应用层没有try cath的错误     

   protected void Application_Error(Object sender, EventArgs e)

        {

            //在出现未处理的错误时运行的代码
            Exception ex = Server.GetLastError();

            Exception exp = ex.GetBaseException();
            if (exp.GetType().Name.Equals("HttpException"))
            {//排除404页面
                if (404 == ((HttpException)exp).GetHttpCode()) { return; }
            }
            StringBuilder sbReqParams = new StringBuilder();
            sbReqParams.Append("\r\nUser-Agent:" + HttpUtility.UrlDecode(this.Request.UserAgent));
            sbReqParams.Append("\r\nForm:"+ HttpUtility.UrlDecode(this.Request.Form.ToString()));
            sbReqParams.Append("\r\nCookies:"+ HttpUtility.UrlDecode(this.Request.Form.ToString()));
            LogHelper.WriteLog(string.Format("应用程序出错:\r\n错误页面:{0}\r\n请求参数:{1}\r\n", this.Request.Url, sbReqParams.ToString()), ex);


            Server.ClearError();
        }
posted @ 2013-12-25 10:59  顿金  阅读(162)  评论(0编辑  收藏  举报