Use asp.net to write Web page error information to the system log

Source: Internet
Author: User
Tags config iis implement integer log php and system log
Asp.net| Error | Web Design site, it is not possible that our site does not appear a little wrong, the common "page does not exist" "Page run Error" and other error information general website How many always exist, the key is, these errors appear, the administrator how to facilitate timely discovery of them, Minimize the user's bad impression of the site. Whether IIS 4 or IIS 5, we can set the site's "Custom error message", through this setting, some of the system defaults to the more unfriendly error messages can be replaced by the site administrator customized pages, which for the site's practical and friendly to help greatly. However, we found a more inconvenient problem in the course of use, when we looked at the site log, we found these error pages appear, but not in the system events to view these error messages. And in the log section of the site to see the error information is more cumbersome, there is no way to directly generate the error message like the security log in the system log part of it? Asp. NET can be done now. Now, let's learn how to do this in a step-by-step way.
First, the establishment of EventLog virtual directory
To implement, we build a virtual directory named "EventLog" in our website, which is as follows: In Win2000, open the "Start"-> "program"-> "Administrative Tools"-> "Internet Information Services" to find the established web site, Click "New" in the Mail, select "Virtual directory" in the pop-up menu, and then follow the wizard settings.
Ii. Modification of Web.config documents
We know that in the Web.config file, we can set the location of the error message page and the error message to display, like the "Custom error Message" page of IIS. In order to implement the functionality mentioned in this article, we need to modify the Web.config file appropriately and turn on "customErrors mode" to "on" so that the computer users can only get friendly (custom) error messages, as set out below:
<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 see that when the 404 and 403 errors are generated, the page will be transferred to the corresponding page we set up the EventLog virtual directory.
Iii. Establishment of other documents
To test the success of our setup, first we have to set up a page default.aspx that can generate errors, the code for this page is 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

' Generates an error
z = x/y
End Sub
</script>

<body>
<form method= "POST" action= "eventlog.aspx" Name= "Form1" id= "number" >
<asp:button id= "Abutton" type= "Submit" text= "Click to generate Error" runat= "Server"/>
</form>
</body>
The above code we designed a zero error page, obviously, when the button is submitted this page is definitely wrong, we need to see if the error will be added to the system log. Now, let's look at the code for the error page, where we might use three error pages customerrorpage.aspx, 404page.aspx, 403page.aspx, all of which are placed under the virtual directory EventLog. Their code is as follows:
Customerrorpage.aspx Code
<body>
</body>
404page.aspx (no error found on page) code
<body>
</body>

403page.aspx (permission error) code
<body>
</body>
After setting the above page, the most important, we need to set the global configuration file global.asax, so that the error message can be saved to the system log, we see how this file settings.
<%@ 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's name ' MyLog '
Dim LogName as String = "MyLog"
Dim message as String = "Url" & Request.path & "Error:" & Errmessage

' If the log does not exist, create a
If (not eventlog.sourceexists (LogName)) Then
EventLog.CreateEventSource (LogName, LogName)
End If

Dim Log as New EventLog
Log.source = LogName

' The v error is 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 above settings, it is defined that when an error occurs, the Web server first checks for the existence of a log named "MyLog", and if it does not exist, create one. The error message is then written to the log and saved. In the above settings, we note the following points: (1) The above code, we will be named "MyLog", in the actual application, we can set the log name according to their own requirements, such as "XX's website log" and so on, so that not only easy to distinguish, but also will not be other administrators mistaken for other content (2) The error page we give is very simple, it is a simple statement. In the actual Web application, we can have a few choices, first of all, we can set all these error pages as the homepage of the site, when the page error or the page does not exist, direct users to the home page, so do not display error messages, for users may browse feel better; You can also set the error page to be more user-friendly, preferably in a way that the average user can understand rather than a simple English hint.
Four, view the effect
Now, we open "event view" to verify that the event is written to the log. To open the program-> Management-> Event Viewer and select MyLog, we can see the following interface (figure I):

In the diagram above, we saw several events inside the MyLog, open the view, and we can see the error message the exact comments are recorded (Figure II):

V. Summary
Above, we use a complete example to explain the error information written to the system log implementation method. After this implementation, I believe that the system Management and Web site management can play a very good role in helping. At the same time, in the process of practical asp.net, we have to marvel at the perfection of its function, asp.net not only the page design, database access, caching and other aspects of the functions and performance greatly improved, but also in conjunction with the system, the overall environmental management and other aspects are further improved. Therefore, for the custom ASP or PHP and other language programming users, in the use of ASP.net, can not only focus on page implementation and data processing and other conventional operations, but should be further in the site security, system management and other aspects of learning asp.net, really in the network to achieve a higher level of development.

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.