Global.asax捕获处理系统异常,插入日志表

本文介绍了一个基于Global.asax的应用程序错误处理机制,通过捕获系统异常并将其详细信息记录到日志表中,实现了有效的错误跟踪与管理。此外,还提供了根据配置选择是否记录异常的具体实现。
Global.asax捕获处理系统异常,插入日志表
 protected void Application_Error(object sender, EventArgs e)
        {
            # region 系统异常处理

            Exception ex = Server.GetLastError();//捕获应用程序的异常
            while (ex.InnerException != null)
            {
                ex = ex.InnerException;
            }
            string strMessage = ex.Message;
            string strStackTrace = ex.StackTrace;
            string strUrl = Request.Url.ToString();
            bool needExLog = bool.Parse(ConfigurationSettings.AppSettings["NeedExLog"]);
            if (needExLog)
            {
                # region 记录异常日志

                BMS_MMS.Model.Log_Exception model = new BMS_MMS.Model.Log_Exception();
                model.PageUrl = strUrl;
                model.Message = strMessage;
                model.StackTrace = strStackTrace;
                model.UserID = BMS_MMS.BLL.Sys_User.GetCurUser().ID.ToString();
                model.LogDate = System.DateTime.Now;
                BMS_MMS.BLL.Log_Exception bll = new BMS_MMS.BLL.Log_Exception();
                bll.Insert(model);

                # endregion

                Response.Redirect("~/Error.aspx?ID=" + model.ID);
            }
            else
            {
                strMessage = Server.UrlEncode(strMessage);
                Response.Redirect("~/Error.aspx?MSG=" + strMessage);
            }

            # endregion
        }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值