动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   .Net → 阅读文章

 显示指定的错误页面,同时把错误信息写入系统日志文件

作者:yirun 来源:开发者俱乐部 
阅读 4867 人次 , 2001-12-10 

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.
}
  

 本文Tags日志  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:Application事件的执行顺序
· 下一篇:选择文件夹的对话框
· .NET框架类览胜(上篇)
· 在 Visual Basic .NET 中实现后台进程(三)
· VS.NET RC5 EA版中所带Visio的UML能力测试报告
· 菜鸟模仿duwamish开发时常见的错误
· 一个用c#写的扫描asp源码漏洞的应用程序


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077