asp.net页面出错时的处理方法

1.第一种做法,在Web.config文件配置

< system.web >
      
< customErrors defaultRedirect = " ~/ErrorPage.aspx "  
                     mode
= " RemoteOnly " >
      
</ customErrors >
</ system.web >

 

  defaultRedirect属性用来指明当aspx页面发生了未处理错误时导向的页面; 但Asp.net使用重定向机制来重新导航错误页面,这样错误信息就会丢失,也就是说我们用Server.GetLastError()获得的Exception对象始终是空的。虽然可以提示用户出错,并提供一个返回出错页面的链接,却不能给管理员一个很好的错误提示。

2.第二种做法:在global文件里的Application_Error方法中处理

代码
protected   void  Application_Error(Object sender, EventArgs e)
        {
            Exception ex
= Server.GetLastError().GetBaseException();

             string  errorTime = " 发生时间: " + DateTime.Now.ToString();
             string  errorAddress = " 发生异常页: " + Request.Url.ToString();
            
string  errorInfo = " 异常信息: " + ex.Message;
            
string  errorSource = " 错误源: " + ex.Source;
            
string  errorTrace = " 堆栈信息: " + ex.StackTrace;
            Server.ClearError();

            System.IO.StreamWriter writer
= null ;
            
try
            {
                
lock ( this )
                {
                    
// 写入日志 
                     string  year = DateTime.Now.Year.ToString();
                    
string  month = DateTime.Now.Month.ToString();
                    
string  day = DateTime.Now.Day.ToString();
                    
string  path = string .Empty;
                    
string  filename = DateTime.Now.ToString( " yyyyMMdd " ) + " .txt " ;
                    path
= Server.MapPath( " ~/Error/ " ) + year + month + day;
                    
if ( ! Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    System.IO.FileInfo file
= new  FileInfo(path + " / " + filename);
                    writer
= new  StreamWriter(file.FullName, true ); // 文件不在则创建,true表示追加
                    writer.WriteLine( " 用户IP: " + Request.UserHostAddress);
                    writer.WriteLine(errorTime);
                    writer.WriteLine(errorAddress);
                    writer.WriteLine(errorInfo);
                    writer.WriteLine(errorSource);
                    writer.WriteLine(errorTrace);
                    writer.WriteLine(
" ------------------------------------------------------- " );

                }
            }
            
finally
            {
                
if (writer != null )
                {
                    writer.Close();
                }
            }
            Server.Transfer(
" ~/ErrorPage.aspx " ); // 转到显示友好错误的页面

        }

然后在ErrorPage.aspx页面显示一些好友的提示信息.

3.第三种做法:在Page_Error事件里面处理

代码
         private   void  Page_Load( object  sender, System.EventArgs e)
        {
            
throw ( new  ArgumentNullException());
        }

        
public   void  Page_Error( object  sender,EventArgs e)
        {
            Exception ex
= Server.GetLastError().GetBaseException();

            
string  errorTime = " 发生时间: " + DateTime.Now.ToString();
            
string  errorAddress = " 发生异常页: " + Request.Url.ToString();
            
string  errorInfo = " 异常信息: " + ex.Message;
            
string  errorSource = " 错误源: " + ex.Source;
            
string  errorTrace = " 堆栈信息: " + ex.StackTrace;

            Server.ClearError();

            System.IO.StreamWriter writer
= null ;
            
try
            {
                
lock ( this )
                {
                    
// 写入日志 
                     string  year = DateTime.Now.Year.ToString();
                    
string  month = DateTime.Now.Month.ToString();
                    
string  day = DateTime.Now.Day.ToString();
                    
string  path = string .Empty;
                    
string  filename = DateTime.Now.ToString( " yyyyMMdd " ) + " .txt " ;
                    path
= Server.MapPath( " ~/Error/ " ) + year + month + day;
                    
if ( ! Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    System.IO.FileInfo file
= new  FileInfo(path + " / " + filename);
                    writer
= new  StreamWriter(file.FullName, true ); // 文件不在则创建,true表示追加
                    writer.WriteLine( " 用户IP: " + Request.UserHostAddress);
                    writer.WriteLine(errorTime);
                    writer.WriteLine(errorAddress);
                    writer.WriteLine(errorInfo);
                    writer.WriteLine(errorSource);
                    writer.WriteLine(errorTrace);
                    writer.WriteLine(
" ------------------------------------------- " );

                }
            }
            
finally
            {
                
if (writer != null )
                {
                    writer.Close();
                }
            }

            Server.ClearError();
// 防止错误继续到要被处理的 Application_Error 事件中。
            Response.Redirect( " ~/ErrorPage.aspx " );
            
        }

 

我经常的做法是使用第二种方法,然后再写一个发送短信的方法(调用移动的短信借口),这样的话程序出错的时候,管理员可以收到程序出错的信息。

你可能感兴趣的:(asp.net)