11.在Global的Application_Error处理错误示例
2013-07-06 11:18
375 查看
Application_Error是在程序出问题时触发的事件。
这里面要用到错误页的情况,所以要配置web.config的customError项。
1.建立Global文件,在它的Application_Error中写入以下代码(TextFile1.txt 是要记录出错信息的日志):
2.建立两个错误页,一个为默认的错误页,另一个为404状态的错误页,即页面没有被找到,
默认错误页error.htm
找不到文件错误页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,具体说明可以看错误页章节
4.建立一个webform面,在它的page_load事件中写如下cs代码:
此时运行时就会显示错误页,打开TextFile1.txt后会出现日志已记录在文件中,如下图日志记录
这里面要用到错误页的情况,所以要配置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后会出现日志已记录在文件中,如下图日志记录
相关文章推荐
- global文件里Application_Error方法处理记录应用程序错误日志
- 在系统出现未处理的错误时,在Global的Application_Error记录下错误
- 在Global中Application_Error事件处理错误信息
- 在系统出现未处理的错误时,在Global的Application_Error记录下错误
- asp.net错误处理Application_Error事件示例
- asp.net错误处理Application_Error事件示例
- 在Global中Application_Error事件处理错误信息
- 利用Global.asax的Application_Error实现错误记录,错误日志
- [置顶] 利用Global.asax的Application_Error实现错误记录,错误日志
- ASP.NET MVC中注册Global.asax的Application_Error事件处理全局异常
- Global.asax的Application_Error实现错误记录/错误日志的代码
- ASP.NET MVC中注册Global.asax的Application_Error事件处理全局异常
- Flex全局错误处理Global Error Handler代码兼容运行于低版本Flash Player
- 怎样处理Svchost.exe Application Error错误
- [Android_Bug]处理PhoneGap Application Error: "The connection to the server was unsuccessful.(file:///android_asset/www/index.html)"的错误
- ASP.NET错误处理方法归结(方法2):Application_Error 事件
- 网狐荣耀版或其它安卓项目出现Application cannot be exported due to the error(s) below.错误的处理方法
- 处理phonegap(cordova) application error the connection to the server was unsuccessful的错误
- Flex全局错误处理Global Error Handler在AIR 2.0和Flash Player 10.1中使用
- RAISE_APPLICATION_ERROR错误处理