带有 customErrors 的 elmah 不会发送电子邮件,除非是 404

发布于 2024-12-04 17:28:19 字数 364 浏览 1 评论 0原文

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

奇怪的是,当我浏览到我的网站上不存在的页面时,我被重定向到主页(正如我在自定义错误中设置的那样),但我确实收到了电子邮件......这可能会出现问题,因为我不想当人们访问我的网站并在网址末尾添加“whatever.php”时,我会收到十亿封电子邮件。

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

I am playing around with Elmah to see if I want to use that as my error handling solution. I installed it, hard coded an exception into my page, hit the page wholla! Got my email, everything is happy. However, when I added the customError node to my web.config to redirect to a friendly error page, the email was not sent but I was redirected to my friendly error page.

Strangely, when I browsed to a page that doesn't exist on my site, I was redirected to home (as I set in my customErrors) but I DID receive the email...that could be problematic as I don't want to get a billion emails when people hit my site and add "whatever.php" to the end of the url.

So I have two questions: 1) why would the exception that is being thrown NOT send me an email and 2) how can I tell Elmah NOT to send me emails for 404s?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残龙傲雪 2024-12-11 17:28:19

您可以在 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="[email protected]" to="[email protected], [email protected]" 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="[email protected]">
        <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

You can filter things with elmah like such in your Global.asax.cs:

    //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();
            }
        }
    }

So add the call to FilterError404 to any part of the filtering. The above example will have it filter 404 for both ErrorLog and Email. Also check out:
http://code.google.com/p/elmah/wiki/ErrorFiltering

You can also do Filtering By Source as described in the link:

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

Check 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="[email protected]" to="[email protected], [email protected]" 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="[email protected]">
        <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>

FYI: NuGET Package also available as explained by Scott Hanselman:
http://www.hanselman.com/blog/NuGetPackageOfTheWeek7ELMAHErrorLoggingModulesAndHandlersWithSQLServerCompact.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文