职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 1480|回复: 0

ASP.net错误处理

[复制链接]
joe 发表于 2006-12-18 21:46 | 显示全部楼层 |阅读模式
ASP.net错误处理-显示指定的错误页面,同时把错误信息写入系统日志文件

asp.net中当服务器出错时显示指定的错误页面同时把错误信息写入系统日志文件的探讨

一,在Web.config中填写出错时显示的页面,可以根据不同的statusCode显示不同的出错页面。
<customErrors mode=\"On\" //如果设置为Off则出错只返回错误信息,不会跳到自己的指定页面defaultRedirect=\"/error/customerrorpage.aspx\">
<error statusCode=\"404\" redirect=\"/error/404Page.aspx\"/>
<error statusCode=\"403\" redirect=\"/error/403page.aspx\"/>
</customErrors>

二,在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.
}
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-11 17:52 , Processed in 0.119181 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表