Using ASP. NET to write webpage error messages to system logs-simple

Source: Internet
Author: User

When designing a website, it is impossible for our website to have no errors. common error messages such as "page does not exist" and "Page Operation error" always exist. The key is, after these errors occur, the administrator can easily discover them in a timely manner and minimize users' bad impression on the website. Whether it is IIS 4 or IIS 5, we can set the "custom error message" of the website, some of the default unfriendly error messages of the system can be changed to the custom page of the website administrator, which is helpful for the practicality and friendliness of the website. However, we found an inconvenient problem during use. When we checked the website logs, we found these error pages. However, however, you cannot view these error messages in system events. It is troublesome to view the error information in the log Part of the website. Is there a way to directly store the generated error information in the system log part like the security log? ASP. NET can be implemented now. Now, we will learn how to implement this function step by step.
1. Create an EventLog virtual directory
Implementation: create a virtual directory named "EventLog" on our website. Cube In Win2000, choose Start> Program "->" Administrative Tools "->" Internet Information Service ", find the created website, click" new "in the email, and select" virtual directory "in the pop-up menu ", then follow the wizard settings.
2. Modify the Web. config file
We know that in the web. config file, we can set the location of the error information page and whether to display the error information like the "custom error information" page of IIS. To implement the functions mentioned in this article, we need to modify the web. in the config file, open "mermerrors mode" and set it to "ON". Non-computer users can only get friendly (custom) error information. The specific settings are as follows:
<Configuration>
<System. Web>
<Customerrors mode = "on" defaultredirect = "/EventLog/customerrorpage. aspx">
<Error statuscode = "404" Redirect = "/EventLog/404page. aspx"/>
<Error statuscode = "403" Redirect = "/EventLog/403page. aspx"/>
</Customerrors>
</System. Web>
</Configuration>
In the above settings, we can see that when errors 404 and 403 are generated, the page will be transferred to the corresponding page where we set the EventLog virtual directory.
3. create other files
To test whether our settings are successful, we must first set up a page default. aspx that can generate errors. Code As follows:
Default. aspx Page code
<% @ Language = "VB" %>
<Script language = "VB" runat = Server>
Sub page_load (sender as object, e as eventargs)
If ispostback then
'Define variables
Dim X as integer
Dim y as integer
Dim Z as integer

X = 1
Y = 0

'Generation Error
Z = x/y
End sub
</SCRIPT>










the above code is used to design a zero-Division Error Page, obviously, this page must have an error when the button is submitted. We need to check whether the error will be added to the system log. Now let's look at the error page code. Here, we may use three error pages: mermerrorpage. aspx, 404page. aspx, and 403page. aspx. These pages are all placed under the virtual directory EventLog. Their code is as follows:
customerrorpage. aspx Code



custom error page




404page. aspx (error not found on the page) code



404 error page



403page. aspx (permission error) Code
<HTML>
<Head> <Body>
<H1> 403 error page </Body>
</Html>
After setting the above page, the most important thing is to set the global configuration file global. asax so that the error information can be saved to the system log. Let's see how to set this file.
<% @ Import namespace = "system" %>
<% @ Import namespace = "system. Diagnostics" %>
<Script language = "VB" runat = Server>

Public sub application_onerror (sender as object, e as eventargs)
'Catch Error
Dim lasterror as exception = server. getlasterror ()
Dim errmessage as string = lasterror. tostring ()

'Here, set the log name to "mylog"
Dim LOGNAME as string = "mylog"
Dim message as string = "url" & request. Path & "error:" & errmessage

'If the log does not exist, create
If (not EventLog. sourceexists (LOGNAME) then
EventLog. createeventsource (LOGNAME, LOGNAME)
End if

Dim log as new EventLog
Log. Source = LOGNAME

'Five errors are listed below
Log. writeentry (message, eventlogentrytype. Information, 1)
'Log. writeentry (message, eventlogentrytype. error, 2)
'Log. writeentry (message, eventlogentrytype. Warning, 3)
'Log. writeentry (message, eventlogentrytype. successaudit, 4)
'Log. writeentry (message, eventlogentrytype. failureaudit, 5)
End sub
</SCRIPT>
In the preceding settings, when an error occurs, the web server first checks whether there is a log named "mylog". If not, create a log. Then, write the error information to the log and save it. In the preceding settings, pay attention to the following points: (1) in the above Code, we name the log as "mylog". In actual application, we can set the log name according to our requirements, such as "×× website log". This will not only be easily identified, but will not be recognized by other administrators as anything else. (2) the above error page is very simple, it is a simple statement. In actual website applications, we have several options. First, we can set all these error pages as the homepage of the website. When the page fails or does not exist, direct the user to the home page of the website, so that no error information is displayed, which may make the user feel better. In addition, you can also set the error page to be friendly, it is better that the average user can understand the method rather than a simple English prompt.
Iv. view results
Now, open "event view" to verify whether the event is written to the log. Open "program"-> "management"-> "Event Viewer" and select "mylog". We can see the following interface (Figure 1 ):

In, we can see several events in mylog. Open and view, and we can see that the error information is recorded (Figure 2 ):

5. Summary
the preceding section describes how to write error messages to system logs using a complete example. After this implementation, I believe it can be of great help to both system management and website management. At the same time, in the practical ASP. in the process of net, we have to marvel at the perfection of its functions again and again, Asp. net not only greatly improves the functions and performance of page design, database access, cache, but also further improves the integration with the system and the management of the entire environment. Therefore, users who are used to programming in ASP, PHP, and other languages are using ASP. net, instead of simply focusing on page implementation, data processing, and other common operations, you should learn ASP in website security, system management, and other aspects. net, truly reaching a higher level in network development.

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.