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:
- void Application_Error(object sender, EventArgs e)
- {
- // Code that runs when an unhandled error occurs
- }
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
- Exception error = Server. GetLastError (). GetBaseException ();
- // An exception is recorded in the event log.
- If(! EventLog. SourceExists ("ApplicationException"))
- {
- EventLog. CreateEventSource ("ApplicationException","Application");
- }
-
- EventLog eventLog =NewEventLog ();
- EventLog. Log ="Application";
- EventLog. Source ="ApplicationException";
- EventLog. WriteEntry (error. ToString (), EventLogEntryType. Error );
-
- // Send an Email to the developer
- MailMessage email =NewMailMessage (Administrator@xiaoyang.com",
- Vince.varallo@PoweredByV2.com");
- Email. Body = error. ToString ();
- Email. Subject ="An error occurred in the Application";
- SmtpClient smtpClient =NewSmtpClient ("127.0.0.1", 25 );
- SmtpClient. Send (email );
- 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:
- <%@ Import Namespace="System.Diagnostics" %>
- <%@ 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.
- Analysis of ASP. NET annotations
- Analysis on the use of ASP. NET trace
- Analysis on the use of ASP. NET Request objects
- Analysis on adding watermarks to Images Using ASP. NET (VB)
- Introduction to ASP. NET page framework