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后会出现日志已记录在文件中,如下图日志记录

最新文章

  1. Mybatis传入参数类型为Map
  2. Freemarker中日期时间格式出错
  3. Python 学习---------Day6
  4. javaWeb总结——session
  5. saltstack之基础入门系列文章简介
  6. ubuntu下chromium 安装flash player
  7. Study on Algorithm of Selecting Safe Landing Area on Ground During Asteroid Soft Landing (EEIC2013 +161)
  8. Hibernate-Native SQL
  9. 《sort命令的k选项大讨论》-linux命令五分钟系列之二十七
  10. 解决 SQL Server 耗尽内存的情况
  11. [转]SQL Server中临时表与表变量的区别
  12. 剑指offer编程题Java实现——面试题11数值的整数次方
  13. TP5 数组分页
  14. Java之修改文件内容:字符串逐行替换
  15. Hive介绍和Hive环境搭建
  16. HUE配置文件hue.ini 的zookeeper模块详解(图文详解)(分HA集群)
  17. FocusBI:《DW/BI项目管理》之SSIS执行情况
  18. HDU 1711 Number Sequence (KMP简单题)
  19. git如何到精通
  20. Codeforces 864E dp

热门文章

  1. chrome vim插件vimnum快捷键与使用
  2. samba环境搭建
  3. ASP.NET 资料下载
  4. css3中允许单词内断句word-wrap和怎么处理断句word-break
  5. 多个互相有联系的checkbox的单选逻辑
  6. Ubuntu Server下建立VPN服务器 pptp 模式的方法
  7. Oracle 11g-R2 SQL Developer连接MSSQL2008
  8. Maven3(笔记二)
  9. 前端开发bower包管理器
  10. 配置hive元数据库mysql时候出现 Unable to find the JDBC database jar on host : master