Why jErrorLog?
jErrorLog is a generic error logging component that I wrote to track and report
exceptions encountered in my applications. It's primarily for use with web applications
although I've used it with services and fat client apps as well.
I have open-sourced jErrorLog. The source code for jErrorLog is available on the
CodePlex web site at http://www.codeplex.com/jErrorLog/.
If you have any reactions pro or con, or suggestions for improvement, I would appreciate
hearing from you. You may contact me here.
jErrorLog supports these different methods of logging and reporting errors:
- Email: An email alert can be sent out when an exception occurs, with information about the exception.
- Text File: Information about the error can be written to a text log file. Permissions are critical here and
the text file must be in a directory to which the IIS has write permissions.
- The NT Event Log: Exceptions can be written to the application event log.
- Database: Events can be written to database tables. This is convenient for tracking of exception trends.
IE: How often does an exception occur? What times of day? Sometimes all the information necessary for
tracking down and eliminating the source of an exception is not immediately available. Note that while jErrorLog
can write information about the event to database tables, I haven't yet created reports of any kind as
a friendly way to view this information. For the moment, ad-hoc queries are the reporting mechanisim.
- Message Queue: Finally, information about exceptions can be posted to a message queue. In the event that
exceptions are caused by lack of availability of the database, posting them to a message queue can ensure that
they're recorded.
Using jErrorLog
To use the component globally to catch all untrapped errors in your web application, you can add the following code to
your application's global.asax file.
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Fires when an error occurs
Dim oEX As Exception = Server.GetLastError.GetBaseException
Dim oLog As New ErrorLog.Log(oEX)
End Sub
Exceptions can also be trapped locally, as follows:
Private moLog As New ErrorLog.Log
Public Sub DoSomeStuff()
Try
throw new exception("Can't do that yet!")
Catch oEX as Exception
moLog.LogMessage(oEX)
End Try
End Sub
Setting up jErrorLog
jErrorLog consists of an assembly that must be referenced by the application. If jErrorLog is to be used with
a database, then the following schema elements must be added to the database (in this order)
A script for creating these tables and stored procedures can be downloaded here.
| Tables |
| 1 |
NextIDValue |
A lookup table used to obtain the next numeric unique id when a record is added to any of the following tables. |
| 2 |
ErrorApp |
Contains a list of applications that have reported errors using jErrorLog |
| 3 |
ErrorModule |
Contains a list of modules (files) that have reported errors using jErrorLog |
| 4 |
ErrorProcedure |
Contains a list of procedures that have reported errors using jErrorLog |
| 5 |
ErrorEvent |
Contains information about individual events such as source and exception type. |
| Stored Procedures |
| 6 |
p_GetNextIDValue |
A stored procedure that retrieves the next available unique id number for the specified kind of record. |
| 7 |
p_ErrorGetAppIDByName |
Retrieves the unique ID associated with an application name as reported by jErrorLog from the ErrorApp table.
If no matching record is found in ErrorApp then a new record is created. |
| 8 |
p_ErrorGetModuleIDByName |
Retrieves the unique ID associated with a module name as reported by jErrorLog from the ErrorModule table.
If no matching record is found in ErrorModule then a new record is created. |
| 9 |
p_ErrorGetProcedureIDByName |
Retrieves the unique ID associated with a procedure name as reported by jErrorLog from the ErrorProcedure table.
If no matching record is found in ErrorProcedure then a new record is created. |
| 10 |
p_ErrorLogEvent |
Stores information about an event. Creates ErrorApp, ErrorModule and ErrorProcedure records as necessary. |
Configuring jErrorLog
The method(s) to be used when logging exceptions are determined by these settings in the web.config or app.config
of the application:
| |
<!-- Error logging -->
|
|
Database connection string. Used to connect to the database that contains the error logging tables.
|
<add key="jErrorLog_con" value="Server=SERVERNAME;DATABASE=DATABASENAME;UID=USERID;PWD=PASSWORD;Application Name=jErrorLog;"/>
|
|
Message Queue connection string. Used to connect to the message queue to which queued errors should be posted.
|
<add key="jErrorLog_MSMQ" value="FormatName:DIRECT=OS:[SERVERNAME]\private$\errorlog"/>
|
|
Email address that should be used as the "sender" address of emailed errors.
|
<add key="jErrorLog_SenderEmail" value="from@emailaddress.com"/>
|
|
Email address to which emailed errors should be sent.
|
<add key="jErrorLog_RecipientEmail" value="to@emailaddress.com"/>
|
|
Fully quaified text file to which errors should be logged. Note: IIS should have write permission to this file.
|
<add key="jErrorLog_FileName" value="c:\test.log"/>
|
|
on to post error messages to the message queue, otherwise off.
|
<add key="jErrorLog_LogToQueue" value="off"/>
|
|
on to post error messages to the database, otherwise off.
|
<add key="jErrorLog_LogToDatabase" value="on"/>
|
|
on to log error messages to the text file, otherwise off.
|
<add key="jErrorLog_LogToFile" value="off"/>
|
|
on to send email error message notifications, otherwise off.
|
<add key="jErrorLog_LogToEmail" value="on"/>
|
|
on to post error messages to the NT application event log, otherwise off.
|
<add key="jErrorLog_LogToEventLog" value="off"/>
|