当前位置:首页 > 网络编程 > WEB编程 > ASP.net > 显示指定的错误页面,同时把错误信息写入系统日志文件

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

点击次数:20 次 发布日期:2008-11-26 23:30: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.
源代码网推荐             }
源代码网推荐        

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华