Log Error Web-Pages to Database.

This demo was created by me, (Steve Schofield) for the fact I want to know
what ASP.NET pages are blowing up and how many times. It was tricky
debugging my error page when my custom error page was blowing up
too!! The customerror.aspx page does two things. First it displays a
nice error message. Secondly, it tries to write the error
information to a database table. If the Database is unavailable, the error
page is still displayed. Here is what I did to test this method of
capturing errors and displaying a nice error page.
- Create a Web called DBDown
- Create the application root for the DBDown web
- Create a database called your db (or whatever) and use the SQL Script
to create the table to hold errors
- Place the files in the zip file into the web
- Open your browser and Test the script (http://localhost/dbdown/default.aspx)
- Look in the database, see if the error is logged
- Stop your database server
- Run the script again (http://localhost/dbdown/default.aspx)
- This should still display the error page, however no record will be
added to the database.
Here is the code examples for this demo
Create a web called DBDown (Make sure the application root is setup)
If your unsure how to make an application root. Click
here for a brief picture how to.

Create a database and place the following DDL
CREATE TABLE [dbo].[tblNotFoundPages] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[pagelookedfor] [varchar] (255) NULL ,
[dateoferror] [datetime] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tblNotFoundPages] WITH NOCHECK ADD
CONSTRAINT [PK_tblNotFoundPages] PRIMARY KEY NONCLUSTERED
(
[id]
) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
Config.web placed in the Application Root
Create a file with the entries below, save the file as config.web. Place the
file into the root directory of the application.
<configuration>
<!-- Connection String that is used for the application root -->
<appsettings>
<add key="MyConn" value="server=localhost;uid=sa;pwd=;Database=yourdb"/>
</appsettings>
<!-- Possible values for mode: On, Off, RemoteOnly -->
<customerrors mode="On" defaultredirect="/dbdown/customerrorpage.aspx">
<error statuscode="404" redirect="/dbdown/404Page.aspx"/>
<error statuscode="403" redirect="/dbdown/403page.aspx"/>
</customerrors>
</configuration>
Custom Error Page(s)
CustomError.aspx page code
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
Dim MyConnection As SQLConnection
Dim Config as HashTable
Config = Context.GetConfig("appsettings")
MyConnection = New SQLConnection(Config("MyConn"))
Dim MyCommand As SQLCommand
Dim DateOfHit as DateTime
DateOfHit = Now()
DateOfHit = DateOfHit.Date()
dim URLPathDude as string
URLPathDude = request.querystring("aspxerrorpath")
Dim InsertCmd As String = "Insert into tblNotFoundPages values (@pagelookedfor,
@dateoferror)"
MyCommand = New SQLCommand(InsertCmd, MyConnection)
MyCommand.Parameters.Add(New SQLParameter("@pagelookedfor",
SQLDataType.varchar, 255))
MyCommand.Parameters("@pagelookedfor").Value = URLPathDude
MyCommand.Parameters.Add(New SQLParameter("@DateOfError",
SQLDataType.DateTime, 8))
MyCommand.Parameters("@DateOfError").Value = DateOfHit
'Use a Try/Catch Block with help if the
'Database is down or unavailable. The error message will still be
displayed
'Make sure the customerror mode="On"
Try
' Open the connection and execute the Command
myConnection.Open()
myCommand.Execute()
Catch Exp As SqlException
'An error occurred, pass the exception up
'I'm not sure of a true Error Number here
'If you place a series of error numbers
'to Check for, this will become a stronger
'part of the code, this is just an example to use!
If Exp.number <> 962 Then
response.write(Exp.number)
End If
Finally
' Close the Connection
If myConnection.State = DBObjectState.Open then
myConnection.Close()
End If
End Try
End Sub
</script>
<h1 align="center">An Error has occurred</h1>
An error has occurred with this ASP+ demo. Please forgive us as the
webmaster has
been notified and will look into the problem ASAP!
404page.aspx
<html>
<head>
</head>
<body>
<h1>404 Error page</h1>
</body>
</html>
403page.aspx
<html>
<head>
</head>
<body>
<h1>403 Error page</h1>
</body>
</html>
Testing Pages
Default.aspx -
This page submits to dbdown.aspx page. The dbdown.aspx page does a
division by Zero to raise an error.
<html>
<head>
</head>
<body>
<form method="post" action="dbdown.aspx" name="form1" id="number">
<asp:Button id="abutton" type="submit" text="Click Me to generate an error" runat="server" />
</form>
</body>
</html>
dbdown.aspx
<%@ Page Language="VB" %>
<script language="VB" runat=server>
Sub Page_Load(Sender As Object, E As EventArgs)
'Declare all variables
dim x as integer
dim y as integer
dim z as integer
'set x and y to values to be divided by zero
x = 1
y = 0
'perform the division by zero to generate error
z = x/y
End Sub
</script>
<html>
<head>
</head>
<body>
This will never get displayed!
</body>
</html>
|