Generic error trapping

Hello,
Is there any way of providing an application-wide try-catch or does it have to be done on each web page seperately?
I currently provide a generic error message caught by using customErrors in web.config but it would be nice if the user didn't have to quit the application and re-start.

As an aside, i would also like to the webapp to send information to me via email if an error occurs - any useful functions/ thoughts on how to do this?

Thanks in advance,
mcg
LVL 6
mcgantsAsked:
Who is Participating?
 
Ramesh SrinivasConnect With a Mentor Technical ConsultantCommented:
Yes there is.

In your Global.asax file insert the following into the Application_Error event:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when an error occurs
        Dim ErrorDescription As String = Server.GetLastError.ToString
       
        Dim MMsg As MailMessage = New MailMessage
        SmtpMail.SmtpServer = "intmeddc02"
        MMsg.To = emailadd
        MMsg.From = "iMediaAdmin@intmeds.com"
        MMsg.Subject = Subject
        MMsg.BodyFormat = MailFormat.Html
        Dim strBody = "The user: " & User.Identity.Name & " ran into an error @ " & Now & Chr(13) & "Error: " & ErrorDescription & "."
        MMsg.Body = strBody
        MMsg.Priority = MailPriority.High
        SmtpMail.Send(MMsg)
        MMsg = Nothing
End Sub

If you are using forms authentication then the line will retrieve username: User.Identity.Name

regards,

KS
0
 
Ramesh SrinivasTechnical ConsultantCommented:
Just replace my settings with your own.
0
 
Ramesh SrinivasTechnical ConsultantCommented:
Also, to make sure users do not see any asp.net error descriptions you can make your own html page which is displayed when an error occurs.

In your web.config file:

<customErrors defaultRedirect="error.htm" mode="RemoteOnly" />

error.htm is the page that will be displayed when an error occurs and it could say something like "Admin has been notified - try again later etc.

regards,

KS
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
mcgantsAuthor Commented:
thanks, that's great it works well, but before i finish, do you know if there is a resolution for the first part of my question:
"I currently provide a generic error message caught by using customErrors in web.config but it would be nice if the user didn't have to quit the application and re-start."
i'm thinking of a roll-back kind of thing where once an error has been generated, putting the user back to where they were. maybe i'm hoping for a bit too much, but the email message works a treat :)

cheers,
mcg
0
 
Ramesh SrinivasTechnical ConsultantCommented:
Hmmm. You could put them back but they would still encounter the error if they attmepted to repeat the operation which caused it.

Perhaps you could put a link on your custom error page to take them back manually - or a redirect?

regards,

kS
0
 
mcgantsAuthor Commented:
no worries, i was just trying to keep the app up and running through the error, but the code you've given me for the email was great. thanks!
0
 
Ramesh SrinivasTechnical ConsultantCommented:
okay, no probs.

All the best,

kS
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.