【问题标题】:elmah with customErrors not emailing unless it's a 404除非是 404,否则带有 customErrors 的 elmah 不会通过电子邮件发送
【发布时间】:2023-04-05 18:08:01
【问题描述】:

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

奇怪的是,当我浏览到我网站上不存在的页面时,我被重定向到主页(正如我在 customErrors 中设置的那样),但我确实收到了电子邮件......这可能是个问题,因为我没有当人们访问我的网站并将“whatever.php”添加到 url 的末尾时,不希望收到十亿封电子邮件。

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

【问题讨论】:

  • 顺便说一句 - 将用户重定向到缺少 URL 的主页是一种反模式,应该避免。它可能做的只是让用户感到困惑——“为什么我点击 x 链接时会转到主页”。取而代之的是一个专门的 404 页面,它简单而清晰地解释了问题,并在理想情况下帮助用户找到他们正在寻找的页面(例如,使用站点地图)。我还建议定期查看您记录的 404 错误(或网站管理员工具),以检查您是否有任何损坏的链接等,以帮助首先避免错误。

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


【解决方案1】:

您可以在 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 和电子邮件过滤 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

【讨论】:

  • 非常感谢!这有很大帮助。知道为什么我的异常没有触发电子邮件吗?
  • 好吧,把你的 web.config 和我的比较一下,看看有什么不同。
  • 使用了您的 web.config(添加到缺失的部分),仍然无法正常工作。我在工作中和我的一个朋友谈过,他帮助我完成了工作。我必须添加一些自定义类,然后从 global.asax 调用它们才能实现。非常感谢您的帮助。
  • 太好了,我在第一次实现它时也遇到了一些麻烦,但是在 30 多页(更像是 5 页)之后我就开始了。仅供参考:是一个 NuGet 包,如上所述。当其他人看到这篇文章时,我有没有需要做的事情?
猜你喜欢
  • 2012-01-21
  • 2012-03-30
  • 2013-04-04
  • 2010-09-07
  • 2017-01-12
  • 2013-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多