Elmah没有通过电子邮件发送MVC3中的异常,但会通过电子邮件发送页面错误,404等

时间:2011-12-15 18:10:49

标签: asp.net-mvc asp.net-mvc-3 smtp elmah

我无法弄清楚这一点。 Elmah没有记录Controller内发生的异常。例如,没有为此生成电子邮件:

  [Authorize]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Reply(ReplyViewModel viewModel)
    {

      throw new Exception("Test");
     ....
    }

Web.config看起来像这样:

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="elmah-sql"   >
    </errorLog>-->
    <errorMail from="somewhere@somewhere.com"
       to="errors@somewhere.com"
       subject="Error Subject Line"
       async="true"
       smtpPort="25"
       smtpServer="ourstmpserver">
    </errorMail>
  </elmah>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="2880" />
    </authentication>
    <membership>
      <providers>
        <clear />
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
      </providers>
    </membership>
    <profile>
      <providers>
        <clear />
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
      </providers>
    </profile>
    <roleManager enabled="false">
      <providers>
        <clear />
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>
    <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>
    <httpHandlers>
      <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </httpHandlers>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true">
      <!--<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>
    <handlers>
      <add name="Elmah" path="elmah.axd" verb="POST,GET,HEAD" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

但是,如果我导航到我没有Controller / Action的URL,我会收到Elmah发来的电子邮件。

是什么给出的?当异常发生时我真的需要电子邮件......

2 个答案:

答案 0 :(得分:0)

尝试使用NuGet上提供的Elmah.Contrib.Mvc。

我们使用这个包,并在我们的Global.asax中注册他们的HandleErrorAttribute,如下所示:

private static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    //filters.Add(new HandleErrorAttribute()); // default MVC setting
    filters.Add(new ElmahHandleErrorAttribute()); // from Elmah.Contrib.Mvc
}

答案 1 :(得分:0)

我遇到了完全相同的问题,并最终通过使用调试源来解决它:Download Source

就我而言,Thread.CurrentPrincipalCurrentPrincipal替换为Identity = null,导致异常,因为Elmah正在使用CurrentPrincipal.Identity.Name

Elmah本身内部的所有异常都会被捕获,以防止程序因日志记录异常而崩溃,在这种情况下,原始异常也会丢失。

这只是我的案例中出现问题的一个例子,Elmah本身导致的任何异常都会导致此行为。因此源应该用于调试。另一种方法是查看Trace,因为Elmah正在为Trace创建任何捕获的异常。