|
Introduction
Handling errors gracefully is something every program needs to do well. Ideally, if a user causes an error to occur, the user should be given the information necessary to correct the problem. However, if the problem is program related, the programmer needs to be given the information necessary to correct the problem. This distinction prevents the user from getting cryptic error messages they are powerless to correct.
Creating the Project
The first step in creating a COM component is to open Visual Basic and select New Project / ActiveX DLL.
Project Code
Writing an event to the event log is very straightforward. In fact the functionality is built into the VB application object. The syntax is:
app.LogEvent(LogBuffer as String, [EventType])
Where LogBuffer is the message you want to pass to the event log, and EventType is the type of event you want to occur. The parameters for EventType are as follows:
1 = The “Error” event.
2 = The “Warning” event
4 = The “Information” event
While this code is very simple, it is not supported in ASP. Therefore, we must build a wrapper around the code. To do this, copy the following code into class1.cls:
'This subprocedure is used to write information to the WinNT event log
Public Sub WinNTLog(ByVal SysErrorNum As String, ByVal SysErrDesc As String, _
ByVal SysErrSource As String, _
Optional ByVal UserErrDesc As String, Optional ByVal ErrType As Integer)
'The values you choose to pass the subprocedure will be based
' on how you plan to use the event log. In this case, we will
' be using the event log to document errors. Therefore, I
' have chosen to pass error messages to the subprocedure.
Dim ErrorMsg As String 'Used to store a formatted error message
'Format the error message.
SysErrorNum = "System Error Number: " & SysErrorNum
SysErrDesc = "System Error Description: " & SysErrDesc
SysErrSource = "System Error Source: " & SysErrSource
ErrorMsg = vbCrLf & vbCrLf & SysErrorNum & vbCrLf & SysErrDesc & vbCrLf & _
SysErrSource
If Not IsEmpty(UserErrDesc) Then
UserErrDesc = "Programmer Error Description - " & UserErrDesc
End If
'If a valid value was not passed in, set a default.
If Not (ErrType = 1 Or ErrType = 2 Or ErrType = 4) Then
ErrType = 1
End If
'Write the formatted error to the NT log
With App
If IsEmpty(UserErrDesc) Then
.LogEvent ErrorMsg, ErrType
Else
.LogEvent ErrorMsg & vbCrLf & vbCrLf & _
UserErrDesc, ErrType
End If
End With
End Sub
Testing the Component
Next we’ll create an ASP page to test our component. To do this, copy the following code into an ASP page.
<%@ Language=VBScript %>
<%
on error resume next
dim x
x = 1/0 'This line will generate an error
if err.number <> 0 then
'Show a "friendly" error message to the user.
Response.Write "<h2>You have tried to divide by zero. Please try “ & _
“dividing by a valid number.</h2>"
'Generate an error message for the programmer.
ErrMsg = "Some user tried to divide by zero."
set myObj = server.CreateObject("ErrorHandler.Class1")
myobj.WinNTLog err.number, err.description, err.source, errmsg, 1
set myobj = nothing
Response.End
end if
%>
We can test our component directly from our ASP page by choosing to run the VB program. When we do so, we will be given the following prompt:
Figure 3
We can use this screen to tell VB how we will call the object. Because we are calling the object from an ASP page, we can accept the defaults.
Finally, run the ASP page. This should give us the following error message:
“You have tried to divide by zero. Please try dividing by a valid number.”
If you check the event log however, you will notice that no error message has been written. This is because the application object can only write to the event log from a fully compiled program. The advantage of running the program from the server’s memory before compiling the program is that this allows us to spot any simple syntax or logic errors in our code. So if the program has run with no errors, we can now compile it and run it again from the compiled DLL.
After running the program, we will see the same message in the browser, but if we look in the event log, we should see an error message similar to the following.
Figure 4
About the Author
Toby Patke is a programmer analyst for the Web development team of a Fortune 50 firm. Toby enjoys creating database or LDAP-enabled multitiered Internet, extranet, and Intranet applications using ASP, ADSI, VB, MTS, DCOM, and creative code. When not programming (a very rare occurrence) he enjoys Harry Belafonte, kayaking, and playing craps for big money. His motto is “Let it ride!” He can be reached at toby_patke@hotmail.com
|