10

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>
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
SventoryMang
  • 10,275
  • 15
  • 70
  • 113
  • 1
    +1 because i didn't know Elmah before, it's for `ASP.Net` only(then Tag it)? – Tim Schmelter Feb 24 '11 at 20:34
  • ok, it seems you aren't calling the Elmah method in the static way (Elmah.ErrorSignal....) it looks like you are assigning an object to Elmah.Error signal, maybe try accessing it in the static method Elmah.ErrorSignal.FromCurrentContext().Raise(ex); (or the vb equivilant) – jonezy Feb 25 '11 at 16:24
  • Did you ever come up with solution for this? About to ask a similar question. I want to send the elmah email when I catch an exception (in one place) – BZink Aug 11 '11 at 18:37

2 Answers2

21

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)

jonezy
  • 1,903
  • 13
  • 21
  • This doesn't work as I said in my original post. When I use ErrorSignal to raise it, I still get re-directed to the custom error page – SventoryMang Feb 24 '11 at 21:14
  • that's very strange because I do the exact same thing, and I have an ERror handler in my global.asax.cs and as soon as I trap the error it stop's bubbling up and never get's redirected, could you post some code? – jonezy Feb 24 '11 at 21:16
  • You could explain about the error handler in global.asax? I don't have anything like that. All the elmah settings are in the web.config – SventoryMang Feb 24 '11 at 21:18
  • in my global.asax i have " public void Application_Error(..) " which is where your exceptions end up if you don't catch and handle them in code, this is where I also redirect to an error page as well – jonezy Feb 24 '11 at 21:21
  • 1
    I don't have anything like that and my uncaught errors get fine. Pretty sure because of my web.config, but I am not sure what I need to change then. – SventoryMang Feb 24 '11 at 21:34
  • can you post your web.config? This is really confusing as the whole point of "catching" errors is to prevent the behaviour that you are getting! – jonezy Feb 24 '11 at 21:36
  • Edited my post to include any and all references of elmah in the web.config – SventoryMang Feb 25 '11 at 15:56
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

Also smokesignals goes to a 404

The question is tagged as a vb.net question, please have some respect

Billy
  • 45
  • 3
  • 1
    respect for what? I write c# so it was quicker to post an example in that language? Is it about the library? You should be able to use that in vb.net, sorry but your request for respect makes no sense. – jonezy Feb 24 '11 at 21:04
  • @jonezy: why the downvote? This answer has the same content as yours. @Billy: I would upvote to correct this, but I have already hit my voting limit for the day. – Mark Avenius Feb 24 '11 at 21:06
  • 3
    the down vote is for demanding respect what no disrespect was given. – jonezy Feb 24 '11 at 21:08
  • 1
    @jonezy: The voting system in this community is not about personal retribution; it is to show which answers provide the best results for a given question. – Mark Avenius Feb 24 '11 at 21:11
  • 2
    and that's your opinion, it's also to point out to people that making comments like "The question is tagged as a vb.net question, please have some respect" that those types of comments are unproductive and shouldn't/don't need to be made. – jonezy Feb 24 '11 at 21:12
  • 1
    @jonezy: actually, that is not my opinion. That is why the system was set up: http://stackoverflow.com/faq#reputation-gain I am not trying to get into a shouting match here; rather, I am trying to educate you and our new user (@Billy) about how the community works. – Mark Avenius Feb 24 '11 at 21:34
  • 2
    so down voting (what i perceived) to be a disrespectful statement is then a no go? Sorry but i think that down voting disrespectful statements follows in the same vein. His answer was correct and complete and the comment about having respect wasn't needed at all. – jonezy Feb 24 '11 at 21:38
  • 2
    again, not about personal retribution, it's about pointing out that the answer was enough and the smart ass comment went to far. – jonezy Feb 24 '11 at 21:40
  • 3
    Fight Fight Fight Fight!!! => http://profile.ak.fbcdn.net/hprofile-ak-snc4/41606_2346078629_3365458_n.jpg – Pure.Krome Sep 02 '11 at 08:21