Captain Codeman Captain Codeman

Elmah Error Logging with MongoDB Official Driver

Contents

Introduction

This basically takes the work that Pablo M. Cibraro did to use Elmah with the Samus CSharp Driver and converts it to work with the Official 10Gen CSharp Driver instead plus a few additional minor changes:

  1. A capped collection is still used but the maximum size (in bytes) and the document limit can now be set using the ‘maxDocuments’ and ‘maxSize’ parameters in the configuration. By default the limit is based on size only with 100mb allocated.

  2. The paged-results for the Elmah reporting page are sorted in descending order so the latest errors are shown first. This uses the $natural sort order of the capped collection.

  3. I’ve used the native MongoDB ObjectId for the error id which should be slightly faster that using a Guid and sorts better (also, if you were interested in saving a few bytes this stores the date and time too so could avoid saving it separately).

  4. Finally, I’ve use the convention of calling the collection ‘Elmah’ when there is no ApplicationName set and ‘Elmah-ApplicationName’ when it is.

Configuration is very similar (but uses the 10Gen driver connection string format):

<elmah>
    <errorLog type="Elmah.MongoErrorLog, Elmah.MongoDB" connectionStringName="ELMAH.MongoDB" maxSize="10485760" maxDocuments="10000" />
</elmah>
<connectionStrings>
    <add name="ELMAH.MongoDB" connectionString="server=localhost;database=elmah;"/>
</connectionStrings>

The source code and compiled binaries are attached. I’ll look at submitting this to the Elmah codebase.

Thanks again to Pablo for doing the hard work !