不再回头 .net学习日记&资料

我再也不愿听你要求 我受够了你那些自私要求

  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  39 随笔 :: 2 文章 :: 14 评论 :: 0 Trackbacks

ASP.NET错误处理一般有三种处理方式:

1 在页面级错误事件中,在单独页面中的错误。可以在page_error事件中添加处理逻辑,具体如下:

Private   Sub  Page_Error( ByVal  sender  As   Object ByVal  e  As  System.EventArgs)  Handles   MyBase .Error

        
Dim  err  As   String   =   " Error in: "   &  Request.Url.ToString  &   " </p> "  _
                        
&   " Stack Trace Below:</br> "  _
                        
&  Server.GetLastError.ToString
        Response.Write(err)
        Server.ClearError()
    
End Sub


2      在应用程序级的错误事件中,在应用程序中的错误。可以在global.asax文件中的application_error中添加处理  逻辑,具体如下:

  Sub  Application_Error( ByVal  sender  As   Object ByVal  e  As  EventArgs)
 
'  在发生错误时激发
         Dim  err  As   String   =   " <h1>Application Error</h1> "  _
                            
&   " Error in: "  _
                            
&  Request.Url.ToString  &   " </p> "  _
                            
&   " Stack Trace Below:</br> "  _
                            
&  Server.GetLastError.ToString
        Response.Write(err)
        Server.ClearError()
    
End Sub

3     在应用程序配置文件中,为应用程序执行的声明性错误处理,具体如下:

<system.web>
< customErrors  defaultRedirect ="url"  mode ="RemoteOnly" >
        
< error  statusCode ="code"  redirect ="url" ></ error >
        
</ customErrors >
</system.web>


当页面发生错误时,应用程序也应该让管理员或开发人员知道何时何地出现了错误,一般有两种方法。

1    向Event Log 写入事件

Imports  System.Diagnostics
Sub  Application_Error( ByVal  sender  As   Object ByVal  e  As  EventArgs)
        
'  在发生错误时激发
  
        
Dim  PageUrl  As   String   =  Request.Path
        
Dim  ErrorInfo  As  Exception  =  Server.GetLastError()

        
Dim  Message  As   String   =   " Url: "   &  PageUrl  &   " </br> "
        Message 
=  Message  &   "  Error:  "
        Message 
=  Message  &  ErrorInfo.ToString  &   " </br> "

        
Dim  LogName  As   String   =   " MyCustomLog "
        
If  ( Not  EventLog.SourceExists(LogName))  Then
            EventLog.CreateEventSource(LogName, LogName)
        
End   If

        
Dim   Log   As   New  EventLog
        
Log .Source  =  LogName
        
Log .WriteEntry(Message, EventLogEntryType.Error)
    
End Sub


2    发送Email

Sub  Application_Error( ByVal  sender  As   Object ByVal  e  As  EventArgs)
        
'  在发生错误时激发
  
        
Dim  PageUrl  As   String   =  Request.Path
        
Dim  ErrorInfo  As  Exception  =  Server.GetLastError()

        
Dim  Message  As   String   =   " Url: "   &  PageUrl  &   " </br> "
        Message 
=  Message  &   "  Error:  "
        Message 
=  Message  &  ErrorInfo.ToString  &   " </br> "

        
Dim  Mymessage  As   New  MailMessage
        Mymessage.To 
=   " tianhao960@gmail.com "
        Mymessage.From 
=   " tianhao960@gmail.com "
        Mymessage.Subject 
=   " ASP.NET Error "
        Mymessage.BodyFormat 
=  MailFormat.Text

        Mymessage.Body 
=  Message
        SmtpMail.Send(Mymessage)

    
End Sub



 

posted on 2006-06-27 04:35 不再回头 阅读(238) 评论(0)  编辑 收藏 引用 所属分类: .net学习资料&日志
只有注册用户登录后才能发表评论。