Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elmah - Catch an Error, but still log it and send email?

First I found that you can catch it and log it inside a catch, but this doesn't send an email. Then I found out about using the Error Signal class. That worked, however what wasn't apparent from reading, is that it treats the error like normal, so when I signal the error it goes to the custom error page still, I don't want that to happen.

What I want to do is catch that error, log it, send the email, but stay on the page the error happened so I can provide special feedback. I do not want it go to the custom error page.

How can I accomplish this?

EDIT: This is what I have and it redirects me to the custom error page.

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

Edit: Posting my web.config sections that involve Elmah (besides the connection string hah) And there is nothing in my Global.asax file involving 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>
like image 343
SventoryMang Avatar asked Feb 24 '11 20:02

SventoryMang


1 Answers

The below should work (i do the exact same thing you are talking about)

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

and if you want a nice framework for displaying the message you can check out smokesignals (disclaimer: it's my work)

like image 161
jonezy Avatar answered Sep 27 '22 16:09

jonezy