ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running ASP.NET / MVC application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment.
Once ELMAH has been dropped into a running web application and configured appropriately, you get the following facilities without changing a single line of your code:
- Logging of nearly all unhandled exceptions.
- A web page to remotely view the entire log of recoded exceptions.
- A web page to remotely view the full details of any one logged exception.
- In many cases, you can review the original yellow screen of death that ASP.NET generated for a given exception, even with customErrors mode turned off.
- An e-mail notification of each error at the time it occurs.
- An RSS feed of the last 15 errors from the log.
ELMAH is a breeze to set up. Within a few minutes you will have ELMAH up and running on your web application. To begin, download the latest x64 binaries from the ELMAH project. If your systems does not support 64 bit then additionally download the regular binary. Uncompress the downloaded binary and copy the contents on bin folder to you project. ELMAH does not care where it goes as long as you reference the DLL; but putting the DLLs in your applications bin folder is good idea. Next reference the Elmah.DLL. Note if your workstation is not a 64 bit machine, then use the regular dll. Before uploading the application to a cwis server, swap it out for the 64.
In order to deploy in IIS7 it is necessary it is necessary to register ELMAH in the <system.web /> and <system.webServer /> in the web.config.
First add follow lines to the <configSections /> to tell ELMAH to read its configuration from web.config
<!-- ELMAH: Tell it to read setting from web.config --> <sectionGroup name="elmah"> <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" /> <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" /> <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" /> <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" /> </sectionGroup>
Next add the ELMAH file handlers in the <httpHandlers /> and <handlers />.
<system.web /> <httpHandlers> <!-- ELMAH: File handler --> <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" /> </httpHandlers>
<system.webServer /> <handlers> <add name="Elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" /> </handlers>
In the <httpModules /> and <modules /> add the logging modules.
<system.web /> <httpModules> <!-- ELMAH: Logging module --> <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/> </httpModules>
<system.webServer /> <modules runAllManagedModulesForAllRequests="true"> <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/> </modules>
Set up ELMAH to log errors to XML by adding to the line before the closing </configuration>.
<!-- ELMAH: Configuration --> <elmah> <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data" /> <security allowRemoteAccess="1" /> </elmah>
Finally, configure <customErrors />
<customErrors mode="RemoteOnly" defaultRedirect="~/Home/Trouble" />
Since we handle errors by redirecting to the “Trouble” action, we better create the methods in the HomeController.
Function Trouble() Return View("Trouble") End Function
Inside the Trouble view add a simple redirect to the Error file (response.redirect(“error”).
Almost done. We need add one line of code to Global.asax telling it to ignore elmah.axd.
That’s it! Compile your application and publish it to the server. Throw some 404’s and view the results by going to, http://wsnet(dev).colostate.edu/cwis###/elmah.axd.
ELMAH supports additional configuration like email and twitter notices as well as logging errors to a database. See this article for specialized configuration.
Setting Elmah for Email Notices
After you have installed ELMAH successfully, you may wish to to know about errors when they happen. Like all things ELMAH, configuring the application to send email notices is quite easy. Just add the email setting in the <elmah /> configuration.
<errorMail from="email@example.com" to="firstname.lastname@example.org" subject="ELMAH Error" async="true" smtpPort="25" smtpServer="wsnet.colostate.edu" />
Setting Elmah for SQL Logging
To configure ELMAH for SQL logging just replace the <errorLog /> XML configuration with the SQL.
Replace: <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data" /> With <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="Elmah.Sql" />
Then, create the connection string.
<add name="Elmah.Sql" connectionString="data source=sqlprod.colostate.edu;database=YourDatabase;integrated security=false;User ID=cwis###;Password=###"/>