ASP. NET has three methods to handle errors:
1. In a page-level error event, an error occurs on a separate page. You can add the processing logic to the page_error event as follows:
Private Sub page_error () 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. ApplicationProgramErrors in applications. You can add the processing logic to application_error in the global. asax file as follows:
Sub application_error () Sub Application_error ( Byval Sender As Object , Byval E As Eventargs)
' Triggered when an error occurs
Dim Err As String = " <H1> application error " _
& " Error in: " _
& Request. url. tostring & " </P> " _
& " Stack trace below: </BR> " _
& Server. getlasterror. tostring
Response. Write (ERR)
Server. clearerror ()
End sub
3. In the application configuration file, the declarative error handling performed for the application is as follows:
<System. Web>
< Customerrors Defaultredirect = "Url" Mode = "Remoteonly" >
< Error Statuscode = "Code" Redirect = "Url" > </ Error >
</ Customerrors >
</System. Web>
When a page error occurs, the application should also let the administrator or developer know when and where an error occurs. Generally, there are two methods.
1. Write events to Event Log
Imports System. Diagnostics
Sub application_error () Sub Application_error ( Byval Sender As Object , Byval E As Eventargs)
' Triggered when an error occurs
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 send email
Sub application_error () Sub Application_error ( Byval Sender As Object , Byval E As Eventargs)
' Triggered when an error occurs
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. = " 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