elmah与customErrors不通过电子邮件发送,除非它是404

时间:2011-09-15 22:28:43

标签: asp.net-mvc exception-handling elmah

我正在和Elmah一起玩,看看我是否想将它用作我的错误处理解决方案。我安装了它,硬编码异常进入我的页面,点击页面wholla!收到我的电子邮件,一切都很开心。但是,当我将customError节点添加到我的web.config以重定向到友好的错误页面时,电子邮件未被发送,但我被重定向到我的友好错误页面。

奇怪的是,当我浏览到我网站上不存在的页面时,我被重定向到家(因为我在我的customErrors中设置)但我收到了电子邮件......这可能会有问题因为我不知道当人们访问我的网站并在网址的末尾添加“whatever.php”时,我想获得十亿封电子邮件。

所以我有两个问题:1)为什么被抛出的异常不会给我发电子邮件?2)我怎么能告诉Elmah不要给我发送404电子邮件?

1 个答案:

答案 0 :(得分:4)

您可以在Global.asax.cs中使用elmah过滤事物:

    //ELMAH Filtering
    protected void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)
    {
        FilterError404(e);
    }

    protected void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs e)
    {
        FilterError404(e);
    }

    //Dimiss 404 errors for ELMAH
    private void FilterError404(ExceptionFilterEventArgs e)
    {
        if (e.Exception.GetBaseException() is HttpException)
        {
            HttpException ex = (HttpException)e.Exception.GetBaseException();

            if (ex.GetHttpCode() == 404)
            {
                e.Dismiss();
            }
        }
    }

因此,将对FilterError404的调用添加到过滤的任何部分。以上示例将为ErrorLog和Email提供过滤器404。还可以看看: http://code.google.com/p/elmah/wiki/ErrorFiltering

您还可以按照链接中所述进行按来源过滤:

<elmah>
    ...
    <errorFilter>
        <test>
            <and>
                <equal binding="HttpStatusCode" value="404" type="Int32" />
                <regex binding="FilterSourceType.Name" pattern="mail" />
            </and>
        </test>
    </errorFilter>
</elmah>

检查web.config:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
    Note: As an alternative to hand editing this file you can use the 
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in 
    machine.config.comments usually located in 
    \Windows\Microsoft.Net\Framework\v2.x\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>
  <appSettings />
  <!-- ELMAH: Configuration -->
  <elmah>
    <security allowRemoteAccess="1" />
    <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="Elmah.Sql" />
    <errorMail defaultCredentials="true" from="someuser@example.com" to="someuser@example.com, someuser2@example.com" subject="Error (STAGING): {0}" async="true" smtpPort="25" smtpServer="192.168.1.1" userName="smtpUserName" password="smtpPassword" />
  </elmah>
  <connectionStrings>  
    <add name="Elmah.Sql" connectionString="Data Source=192.168.1.1;database=DBName;integrated security=false;User ID=MyUserName;Password=MyPassword" />
  </connectionStrings>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="someuser@example.com">
        <network defaultCredentials="true" host="192.168.1.1" port="25" userName="smtpUserName" password="smtpPassword" />
      </smtp>
      <!-- Use this setting for development
      <smtp deliveryMethod="SpecifiedPickupDirectory">
        <specifiedPickupDirectory pickupDirectoryLocation="C:\Temp" />
      </smtp>
      -->
    </mailSettings>
  </system.net>
  <system.web>
    <!-- 
            Set compilation debug="true" to insert debugging 
            symbols into the compiled page. Because this 
            affects performance, set this value to true only 
            during development.
    -->
    <compilation debug="true">
      <assemblies>
        ...........................
      </assemblies>
    </compilation>

        .......................
    <!--
            The <customErrors> section enables configuration 
            of what to do if/when an unhandled error occurs 
            during the execution of a request. Specifically, 
            it enables developers to configure html error pages 
            to be displayed in place of a error stack trace.
      -->
    <customErrors mode="RemoteOnly" defaultRedirect="~/Home/MyErrorPage" />
  .............................
    <httpHandlers>
        ..............................
      <!--ELMAH-->
      <add verb="POST,GET,HEAD" path="/MyErrorPage/elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </httpHandlers>
    <httpModules>
  ........................
      <!-- ELMAH: Logging module -->
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
      <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
    </httpModules>
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true">
    .............................
    <!-- ELMAH-->
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
      <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
    </modules>
    <handlers>
    ..............
      <!--ELMAH-->
      <add name="Elmah" verb="POST,GET,HEAD" path="/MyErrorPage/elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </handlers>
  </system.webServer>
 ..................
</configuration>

仅供参考:NuGET包也可以按照Scott Hanselman的说明获得: http://www.hanselman.com/blog/NuGetPackageOfTheWeek7ELMAHErrorLoggingModulesAndHandlersWithSQLServerCompact.aspx