Filter exceptions logged in ELMAH
By default, all exceptions are logged in ELMAH's database. You can filter out exceptions and thus exclude them from the logs, for example, to exclude exceptions thrown from a certain IP range address. This address may correspond to your company's IP address and thus exclude logs made by your QAs or developers.
To filter and exclude exceptions from the log, you modify your application's web.config file. The following example demonstrates how to exclude from the logs all exceptions of type HttpStatusCode with value 404:
-
Configure an additional
Elmah.ErrorFilterModulemodule. Add the module after all other ELMAH logging modules:XML<httpModules> ... <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" /> <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" /> <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" /> ... </httpModules> -
In the
configSectionssection, add a dedicated configuration section withElmah.ErrorFilterSectionHandlerfor the filtering assertions:XML<configSections> ... <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> ... </configSections> -
In the
modulessection, add theErrorFilterModule:XML<modules> ... <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" /> <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" /> <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" /> ... </modules> -
In the
elmahgroup, add the<errorFilter>section:XML<elmah> ... <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="elmah-sqlserver" /> <errorFilter> <test> <equal binding="HttpStatusCode" value="404" type="Int32" /> </test> </errorFilter> </elmah>