ASP. NET error handling

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.