0

I've created custom error handling in global.asax, where I handle unhandled exceptions. The custom message is generated and shown and the exception gets logged by ELMAH.

The only problem with this is that ELMAH logs twice. Its exception handling fires before the global Application_Error, where it's logged again.

I need the entry generated in Application_Error because that's the place where an exception GUID is generated, appended to the user message and written in Exception.Data, where it's logged by ELMAH (I extended the logger).

How can I skip ELMAH unhandled exception logging?

InteXX
  • 6,135
  • 6
  • 43
  • 80
berzinsu
  • 915
  • 7
  • 12
  • Can't you remove the `ErrorLog`, `ErrorMail` and `ErrorFilter` modules from the Web.config if you're just planning on using it manualy? – CodingIntrigue Sep 04 '14 at 07:22
  • Then nothing gets logged. – berzinsu Sep 04 '14 at 07:28
  • Do the answers in this Q&A help provide some guidance on how you could perhaps proceed? - http://stackoverflow.com/questions/766610/how-to-get-elmah-to-work-with-asp-net-mvc-handleerror-attribute – Tommy Sep 17 '14 at 01:41

2 Answers2

2

Go to the web.config system.webServer/modules section and comment/remove the elmah http module. This will prevent elmah from unhandled exception logging.

<!--<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />-->

Log all errors into elmah manually in global.asax:

protected void Application_Error()
{
    var lastException = Server.GetLastError();
    var customException = new Exception("custom message", lastException);
    Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(customException));
}

complete web.config example:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <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>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

  <system.web>

    <httpRuntime targetFramework="4.5" />

    <compilation debug="true" targetFramework="4.5" />

    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
    <httpModules>
      <!--<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />-->
      <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
    </httpModules>
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />

    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    <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>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <elmah>
    <!--
        See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for 
        more information on remote access and securing ELMAH.
    -->
    <security allowRemoteAccess="false" />
    <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="elmah-sqlserver" />
  </elmah>
  <location path="elmah.axd" inheritInChildApplications="false">
    <system.web>
      <httpHandlers>
        <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
      </httpHandlers>
      <!-- 
        See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for 
        more information on using ASP.NET authorization securing ELMAH.

      <authorization>
        <allow roles="admin" />
        <deny users="*" />  
      </authorization>
      -->
    </system.web>
    <system.webServer>
      <handlers>
        <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
      </handlers>
    </system.webServer>
  </location>
  <connectionStrings>
    <add name="elmah-sqlserver" connectionString="Data Source=.\SQLEXPRESS2012;Initial Catalog=Elmah;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>
Marian Ban
  • 8,158
  • 1
  • 32
  • 45
  • If section is taken out, then log configuration becomes invalid `` – berzinsu Sep 23 '14 at 07:54
  • @berzinsu ups sorry i didn't notice that you are using sql sever configuration – Marian Ban Sep 23 '14 at 07:59
  • @berzinsu what do you mean by bocomes invalid? for me the elmah still works after i removed the http module. I'm logging to sql too. – Marian Ban Sep 23 '14 at 08:09
  • I get exception, that web.config is invalid due the errorLog tag at the end of it. – berzinsu Sep 23 '14 at 16:06
  • @berzinsu i attached my complete web.config with commented http module sections. I tested it locally and it works for me. – Marian Ban Sep 23 '14 at 16:58
  • @MarianBan — My situation is very similar to OP's, except that I'm logging to file instead of SQL. I tried disabling the logging module as you describe, but that also turns off the ability to log manually. I either get duplicate entries or none. I'm more fully describing the problem [here](https://stackoverflow.com/q/71745714). Do you have any ideas? – InteXX Apr 05 '22 at 02:39
  • @MarianBan — I managed to solve the problem. See my answer [here](https://stackoverflow.com/a/71759542). – InteXX Apr 05 '22 at 23:39
0

I was having the same problem, so I did some research and testing. I found the solution.

It's better to leave your logging statements out of Application_Error altogether, and let ELMAH handle the errors by herself.

More information, including a code sample, is available here.

InteXX
  • 6,135
  • 6
  • 43
  • 80