Search code examples
c#asp.net-mvcelmah

Elmah not recording exception in database


I am trying to record all exceptions raised in application into database by using Elmah. but its not working. whenever Elmah.ErrorSignal.FromCurrentContext().Raise(exception); is called error is not inserted into database

Code snippet

  1. Logger.cs

public class Logger
{
    public static void LogException(Exception exception)
    {
        if (HttpContext.Current == null)
        {
            Elmah.ErrorLog.GetDefault(null).Log(new Error(exception));
        }
        else
        {
            Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
        }
    }

    public static void LogMessage(string message)
    {
        if (ServiceConfiguration.ShouldLogMessage)
        {
            Logger.LogException(new Exception(message));
        }
    }

}

  1. Web.Config

<?xml version="1.0"?>
<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>

  <elmah>
    <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="ErrorLog" />
  </elmah>
  

  <connectionStrings>
    <add name="ErrorLog" providerName="System.Data.SqlClient" connectionString="Data Source=******;Initial Catalog=ETFErrorLog;User Id=sa;Password=******;User Instance=false" />
  </connectionStrings>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
    </httpModules>
    <httpHandlers>
      <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </httpHandlers>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>

</configuration>


Solution

  • try to add

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

    in <modules> element inside <system.webServer> element.

    Uros