Skip to main content
CSharpUniversity ASP.NET and C# lessons

The search runs entirely in your browser over the published lesson list.

Open source

Error Handler project

When a web application fails in front of a real visitor, you need the details on disk and easy to search. ErrorHandler does that job and nothing more.

The problem it solves

Unhandled exceptions in a live web application are invisible unless you happen to be watching. Building your own logging table and viewer takes a day you would rather spend on features. ErrorHandler is the shortcut: a small library that writes the exception to a table, and a viewer that reads the table back.

Two pieces of software

  • The logging library. A class library that exposes a single method, saveError, which your catch block calls with the details of the failure.
  • The viewer site. A small web application that searches and displays the rows that were written, using either a quick view of recent entries or a custom search.

What the saveError method records

The method takes the connection string and the error message as required values, then optional extras: the time, the stack trace, the location in your code, the visitor address and the server name. Five spare fields are left open for anything specific to your application. It returns the new row identifier, which is useful to show a visitor so they can quote it when they call support.

Parameters accepted by the saveError method
ParameterRequiredPurpose
Connection stringYesPoints at the database holding the error table
Error messageYesThe text of the exception
TimeYesWhen the failure happened
Stack traceNoWhere in the code it happened
LocationNoModule or page that raised it
Visitor addressNoWho hit the failure
Server nameNoWhich machine served the request
Five custom fieldsNoAnything specific to your application

Setting it up

  1. Create the error table in a SQL Server database, either by running the supplied script or by restoring the supplied backup.
  2. Run the small test site to confirm the library can connect and write a row.
  3. Point the viewer site at the same database and confirm the rows appear.
  4. Reference the library from your own project and call saveError inside your catch blocks.
Safety note. Keep credentials out of source control and out of any page that visitors can read. Store connection strings in configuration and never in a screenshot or a sample that ships with the code.

Searching the log

The viewer offers a quick view of the most recent entries, a view of everything from the last week, and a custom search across error identifier, message and a date range. You can match exactly or match a prefix, choose newest first or oldest first, and set how many rows come back.

Using it on more than one application

Give each application its own error table and its own instance of the viewer. That keeps one busy application from drowning out the errors of another.

Licence

The project is released as open source so you can adapt it. Read the licence agreement before you redistribute it, and share it with anyone who finds it useful.

New lessons by email

Leave your address and get a short note whenever a fresh lesson is published. No account needed and you can stop at any time.

Thank you. Your address has been noted in this browser session. Connect a mailing service to deliver the notes for real.