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

本文介绍了一种在ASP.NET中实现全局错误处理的方法,通过在Global.ascx文件中定义Application_Error事件来捕获未处理的异常,并记录详细的错误信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

转载于:https://www.cnblogs.com/dashi/p/4034630.html

以下是我项目的入口文件: <?php // echo phpinfo(); // die; /** * @name index.php * @desc 入口文件 * @author boye.liu */ try { // Define path to application directory defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application')); // Ensure /library on include_path, // And library must be the first, to avoid the error: "Fatal error: Cannot redeclare class Zend_Db" set_include_path(implode(PATH_SEPARATOR, array( realpath(APPLICATION_PATH . '/library'), get_include_path(), ))); // Create application, bootstrap, and run $application = new Yaf_Application( APPLICATION_PATH . '/configs/application.ini' ); $application->bootstrap() ->run(); } catch (Exception $ex) { header("Content-Type: application/json;charset=utf-8"); $code = $ex->getCode(); $msg = $ex->getMessage(); Kepler_Log::error($msg); $errArr = array( "errno" => 1, //system error "errmsg" => "系统错误", "data" => array(), ); echo json_encode($errArr); } 以下时application.ini文件: [product] ;=========== php ini setup phpSettings.display_startup_errors = 0 phpSettings.display_errors = 0 phpSettings.date.timezone = "Asia/Shanghai" ;phpSettings.error_reporting = "E_ALL & ~ E_STRICT" ;=========== application application.directory = APPLICATION_PATH "/" application.bootstrap = APPLICATION_PATH "/Bootstrap.php" application.library = APPLICATION_PATH "/library" application.library.namespace = "Zend,Kepler" ;======= 忽略前缀,仅仅当用于分产品线才使用 ;application.baseUri = "/audit" application.dispatcher.throwException=True application.dispatcher.catchException=True application.dispatcher.defaultModule = "index" application.dispatcher.defaultController = "index" application.dispatcher.defaultAction = "index" ;======= 开发添加的module需要添加到这里 application.modules = "Index,Audit,User,Common,Collect,Business,Data,Market,Company,Shop,Goods,Enquiry,Offer,Order,News,Dt,Meeting,Scf,Crm,Journal,Report,Article,Vote" ;======= application.system.* 通过这个属性, 可以修改yaf的runtime configure ;======= use_spl_autoload must be true application.system.use_spl_autoload=1 为啥现在我所有的接口地址都报错404,但是不带接口的话也能进入到入口文件中去?
最新发布
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值