2011-09-15 26 views
1

我正在玩Elmah,看看我是否想用它作为我的错误处理解决方案。我安装了它,硬编码了一个例外到我的页面,击中页面wholla!得到我的电子邮件,一切都很开心。但是,当我将customError节点添加到我的web.config中以重定向到友好的错误页面时,该电子邮件未发送,但我被重定向到友好的错误页面。elmah与customErrors不通过电子邮件,除非它是404

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

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

+0

由于旁路 - 用户重定向到主页的缺少URL是一个ti模式,应该避免。它很可能会让用户感到困惑 - “当我点击x链接时,为什么要进入主页”。相反,有一个专门的404页面,可以简单明了地解释问题,并且最理想地帮助用户找到他们正在寻找的页面(例如使用站点地图)。我还建议定期检查您记录的404错误(或网站管理员工具),以检查是否有任何损坏的链接等,以帮助避免首先出现错误。 – pwdst

回答

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="[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

+0

非常感谢!这帮助了很多。任何想法为什么我的例外不会触发电子邮件? –

+0

好吧,比较你的web.config与我的看看是否有任何差异。 – AhsenB

+0

使用你的web.config(添加在缺少的部分),仍然无法正常工作。我在工作中和我的一个哥们谈过话,他帮我解决了问题。我不得不添加一些自定义类,然后从global.asax中调用它们来实现它。非常感谢您的帮助。 –