Elmah - 捕获错误,但仍然记录并发送电子邮件?

时间:2011-02-24 20:31:25

标签: .net asp.net vb.net elmah

首先,我发现您可以捕获它并将其记录在捕获中,但这并不会发送电子邮件。 然后我发现了使用Error Signal类。然而,有效的是,它从阅读中看不出来的是,它会像正常一样对待错误,所以当我发出错误信号时,它仍然会转到自定义错误页面,我不希望这种情况发生。< / p>

我想要做的是捕获该错误,记录,发送电子邮件,但保留在页面上发生错误,以便我可以提供特殊反馈。我不希望它转到自定义错误页面。

我该如何做到这一点?

编辑:这就是我所拥有的,它将我重定向到自定义错误页面。

    Try
        smtpClient.Send(mailMessage)
    Catch smtpEx As SmtpException
        errorSignal.FromCurrentContext().Raise(smtpEx)
    Catch ex As Exception
        errorSignal.FromCurrentContext().Raise(ex)          
    End Try

编辑:发布涉及Elmah的web.config部分(除了连接字符串hah) 我的Global.asax文件中没有涉及Elmah的内容。

  <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>
<security allowRemoteAccess="1" />
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="Elmah.Sql" applicationName="Web Main" />
<errorMail from="xxx" to="xxx" cc="xxx" subject="main website error" async="true" smtpPort="25" smtpServer="xxx" userName="xxx" password="xxx" />
<errorFilter>
  <test>
    <and>
      <equal binding="HttpStatusCode" value="404" type="Int32" />
      <regex binding="FilterSourceType.Name" pattern="mail" />
    </and>
  </test>
</errorFilter>
</elmah>
 <httpHandlers>
        <add verb="POST,GET,HEAD" path="errors/admin/elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </httpHandlers>
    <httpModules>
        <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
        <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
        <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
    </httpModules>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
      <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
    </modules>
    <handlers>
      <add name="Elmah" path="elmah/admin/elmah.axd" verb="POST,GET,HEAD" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
    </handlers>
    <httpErrors>
            <remove statusCode="404" subStatusCode="-1" />
      <remove statusCode="500" subStatusCode="-1" />
      <error statusCode="500" prefixLanguageFilePath="" path="/errors/error.asp" responseMode="ExecuteURL" />
            <error statusCode="404" prefixLanguageFilePath="" path="/global/404.aspx" responseMode="ExecuteURL" />
    </httpErrors>
  </system.webServer>
<location path="errors/admin/elmah.axd">
    <system.web>
        <authorization>
            <deny users="?" />
        </authorization>
    </system.web>
</location>

2 个答案:

答案 0 :(得分:20)

下面的内容应该有用(我说的与你说的完全一样)

try {
  // do something
} catch (Exception ex) {
  Elmah.ErrorSignal.FromCurrentContext().Raise(ex); // logs and sends the error through elmah
  // write a message to the user
}

如果您想要一个漂亮的框架来显示消息,您可以查看smokesignals(免责声明:这是我的工作)

答案 1 :(得分:1)

Try
'      do something
Catch ex As Exception
    ' logs and sends the error through elmah
    ' write a message to the user
Elmah.ErrorSignal.FromCurrentContext().Raise(ex)
End Try

smokesignals也进入了404

该问题被标记为vb.net问题,请尊重