11.在Global的Application_Error处理错误示例

本文介绍ASP.NET应用程序中的错误处理与日志记录方法,包括使用Global.asax文件中的Application_Error事件来捕获异常并记录日志,以及通过web.config文件配置自定义错误页面。

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

Application_Error是在程序出问题时触发的事件。

这里面要用到错误页的情况,所以要配置web.config的customError项。

1.建立Global文件,在它的Application_Error中写入以下代码(TextFile1.txt 是要记录出错信息的日志):

 protected void Application_Error(object sender, EventArgs e)
        {
            Exception ex = HttpContext.Current.Server.GetLastError();
            File.WriteAllText(HttpContext.Current.Server.MapPath("~/TextFile1.txt"), ex.Message + DateTime.Now.ToShortTimeString());
        }

2.建立两个错误页,一个为默认的错误页,另一个为404状态的错误页,即页面没有被找到,

   默认错误页error.htm

  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
出错啦
</body>
</html>

 找不到文件错误页NotFoundFile.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> 没找到相关文件 </body> </html>

3.设置web.config,具体说明可以看错误页章节

  

<?xml version="1.0" encoding="utf-8"?>

<!--
  有关如何配置 ASP.NET 应用程序的详细消息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
      <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="error.htm">
        <error statusCode="404" redirect="NotFoundFile.htm"/>
      </customErrors>
    </system.web>

</configuration>

4.建立一个webform面,在它的page_load事件中写如下cs代码:

 protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("IP:127.0.0.1;datasource=db1.db");
            conn.Open();
        }

此时运行时就会显示错误页,打开TextFile1.txt后会出现日志已记录在文件中,如下图日志记录

转载于:https://www.cnblogs.com/yagzh2000/p/3174917.html

### Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Failed to process, Error SQL: SELECT COUNT(*) FROM (SELECT t1."id" as detail_id, t1.line_no, t1.sku_code, item.item_code spu_code, t1.plan_amount as detail_plan_amount, t1.complete_amount as detail_complete_amount, t1.reverse_number as detail_reverse_number, t1.standard_price, t1.price_with_no_tax as detail_price_with_no_tax, t1.tax_rate as detail_tax_rate, t1.tax_price as detail_tax_price, t1.price as detail_price, t1.tax_amount as detail_tax_amount, t1.amount as detail_amount, t1.amount_with_no_tax as detail_amount_with_no_tax, t1.amount_with_tax as detail_amount_with_tax, t1.discount_rate, t1.discount_amount, t1.base_unit_code, t1.brand_name, t1.brand_id, t1.remark as detail_remark, t1.warehouse_amount as detail_warehouse_amount, t1.reverse_amount as detail_reverse_amount, t1.gift, t2.*, t7.name as price_type_name, sku.cost_year, sku.season_code, sku.season_name, sku.color_code, sku.color_name, sku.order_size_code, sku.order_size_name, sku.small_class_category_code, sku.small_class_category_name, sku.product_line_code, sku.product_line_name, sku.middle_class_category_code sku.middle_class_category_name FROM tc_purchase_sub_order_line t1 LEFT JOIN tc_purchase_sub_order t2 ON t1.sub_order_code = t2.code AND t2.deleted = FALSE LEFT JOIN cc_price_type t7 ON t7.code = t2.price_type AND t7.deleted = FALSE LEFT JOIN ic_sku sku on sku.sku_code = t1.sku_code and sku.deleted = FALSE LEFT JOIN ic_item item on sku.item_id = item.id and item.deleted = FALSE WHERE t1.deleted = FALSE AND (t2.type = ? AND t2.code LIKE ?)) TOTAL
最新发布
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值