Analysis of Application Exception Handling in ASP. NET project development

Source: Internet
Author: User
Tags mailmessage smtpclient

In ASP. NET project development, what are the issues with application exception handling? Let's start with our explanation:

I believe you are familiar with the Application object and have added the Global. asax file to the project. Indeed, the codes for ASP. NET to handle exceptions at the application level are all stored in the Application_Error event processing of Global. asax:

 
 
  1. void Application_Error(object sender, EventArgs e)  
  2.  {  
  3.   // Code that runs when an unhandled error occurs  
  4.  } 

We can capture all exceptions in the preceding event processing method, record exceptions to log files, and send an Email to inform developers of any problems, as shown below:

Application Exception Handling Code in ASP. NET project development

 
 
  1. Exception error = Server. GetLastError (). GetBaseException ();
  2. // An exception is recorded in the event log. 
  3. If(! EventLog. SourceExists ("ApplicationException"))
  4. {
  5. EventLog. CreateEventSource ("ApplicationException","Application");
  6. }
  7.  
  8. EventLog eventLog =NewEventLog ();
  9. EventLog. Log ="Application";
  10. EventLog. Source ="ApplicationException";
  11. EventLog. WriteEntry (error. ToString (), EventLogEntryType. Error );
  12.  
  13. // Send an Email to the developer 
  14. MailMessage email =NewMailMessage (Administrator@xiaoyang.com",
  15. Vince.varallo@PoweredByV2.com");
  16. Email. Body = error. ToString ();
  17. Email. Subject ="An error occurred in the Application";
  18. SmtpClient smtpClient =NewSmtpClient ("127.0.0.1", 25 );
  19. SmtpClient. Send (email );
  20. Response. Redirect ("ErrorPage. aspx");

Of course, to run the above Code correctly, we must be at Global. add the corresponding namespace to asax and replace the above "127.0.0.1" with the address of our email server when sending the mail:

 
 
  1. <%@ Import Namespace="System.Diagnostics" %>  
  2. <%@ Import Namespace="System.Net.Mail" %> 

Note that ASP. NET is run using an ASPNET account. The permissions of this account are limited. If we want to make the above Code run, we must grant the ASPNET account the permission to access the registry. If you do not grant permissions, the above Code will report an error.

We grant the ASPNET account the permission to access the "HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ Eventlog" node and the byte point.

How to Configure permissions for exception handling of applications in ASP. NET project development:

1. Open the "run" menu

2. Enter "regedit" and then confirm

3. navigate to the "HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ Eventlog" node.

4. Right-click the node and select "permission". The permission Configuration window is displayed.

5. Click "add", click "advanced" in the pop-up window, and then click "Search". In the window below, find the "ASPNET" account and click "OK.

6. Finally, the read permission for the ASPNET account is OK.

In the above Code, if we do not add the Response. Redirect method at the end, after an error occurs, the user will see the very classic yellow-colored error page. We also know that the classic error page exposes a lot of information, so we often navigate to our custom error page.

This section describes how to handle application exception in ASP. NET project development. It is helpful for you to understand the exception handling in ASP. NET project development.

  1. Analysis of ASP. NET annotations
  2. Analysis on the use of ASP. NET trace
  3. Analysis on the use of ASP. NET Request objects
  4. Analysis on adding watermarks to Images Using ASP. NET (VB)
  5. Introduction to ASP. NET page framework

Related Article

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.