使用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(" User-Agent:" + HttpUtility.UrlDecode(this.Request.UserAgent));
            sbReqParams.Append(" Form:"+ HttpUtility.UrlDecode(this.Request.Form.ToString()));
            sbReqParams.Append(" Cookies:"+ HttpUtility.UrlDecode(this.Request.Form.ToString()));
            LogHelper.WriteLog(string.Format("应用程序出错: 错误页面:{0} 请求参数:{1} ", this.Request.Url, sbReqParams.ToString()), ex);


            Server.ClearError();
        }
原文地址:https://www.cnblogs.com/dashi/p/4034630.html