asp.net中当服务器出错时显示指定的错误页

本文介绍ASP.NET中如何配置错误页面及利用Global.asax捕获并记录错误信息到系统日志的方法。通过设置Web.config文件可以针对不同状态码显示特定的错误页面,并在Global.asax中通过Application_Error事件将详细的错误信息写入日志。

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

  src="http://www.joyo.com/?source=ad4all_50624" frameborder="0" width="100%" scrolling="no" height="3000"> asp.net中当服务器出错时显示指定的错误页面同时把错误信息写入系统日志文件的探讨 一,在Web.config中填写出错时显示的页面,可以根据不同的statusCode显示不同的出错页面。 二,在Global.asax文件中添加应用出错代码,写入系统日志文件 protected void Application_Error(Object sender, EventArgs e) { Exception LastError = Server.GetLastError(); String ErrMessage = LastError.ToString(); String LogName = "MyLog"; String Message = "Url " + Request.Path + " Error: " + ErrMessage; // Create Event Log if It Doesn't Exist if (!EventLog.SourceExists(LogName)) { EventLog.CreateEventSource(LogName, LogName); } EventLog Log = new EventLog(); Log.Source = LogName; //These are the five options that will display a different icon. Log.WriteEntry(Message, EventLogEntryType.Information, 1); Log.WriteEntry(Message, EventLogEntryType.Error, 2); Log.WriteEntry(Message, EventLogEntryType.Warning, 3); Log.WriteEntry(Message, EventLogEntryType.SuccessAudit, 4); Log.WriteEntry(Message, EventLogEntryType.FailureAudit, 5); } 三,现在你可以进行测试了。我在Default.aspx.cs中产生一个错误,果然跳到默认的错误页面! private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here try { int y=0; int x=1/y; } catch (Exception Err) { throw new Exception("404");//我想产生不同的错误,对应web.config中的statusCode,该如何实现? //Err. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值