Elmah没有在数据库中记录异常

时间:2014-11-14 09:13:14

标签: c# asp.net-mvc elmah

我正在尝试使用Elmah将应用程序中引发的所有异常记录到数据库中。但它不起作用。每当调用Elmah.ErrorSignal.FromCurrentContext().Raise(exception);时,错误都不会插入数据库

代码段

  1. Logger.cs
  2. 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中
    2. <?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>

1 个答案:

答案 0 :(得分:1)

尝试添加

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

<modules>元素内的<system.webServer>元素中。

乌罗什