I have a site that I want to trap 500 errors. (v6.2.x & 7.x)
We might all consider ourselves excellent programmers and code for every unhandled exception error, but, occasionally one slips through the net.
So, this is what I'm trying to achieve:
A unhandled exception occurs in a piece of code that I've written.
Umbraco logs the error in the normal way (in UmbracoTraceLog)
My handler then emails me with the details of the exception and then
Redirects to an "Ooops... Something went wrong page."
I've trawled this forum and elsewhere for an answer. Seems like a lot of dead end pointers and no real solution.
Ive made a start on this code by creating a class that inherits ApplicationEventHandler
I've overridden the ApplicationStarted medth (which fires when the application is started) but, the umbracoApplication.Error event doesn't seem to fire.
Here's my code:-
Namespace Umbraco.Extensions.EventHandlers
Public Class RegisterEvents
Inherits ApplicationEventHandler
Protected Overrides Sub ApplicationStarted(umbracoApplication As UmbracoApplicationBase, applicationContext As ApplicationContext)
Dim sFile As String = "/test.txt"
Dim oSW As New StreamWriter(HttpContext.Current.Server.MapPath(sFile))
oSW.WriteLine("TRAPPED")
oSW.Close()
AddHandler umbracoApplication.Error, AddressOf Application_Error
End Sub
Public Sub Application_Error(sender As Object, e As System.EventArgs)
Dim sFile As String = "/error.txt"
Dim oSW As New StreamWriter(HttpContext.Current.Server.MapPath(sFile))
oSW.WriteLine("ERROR FIRED")
oSW.Close()
End Sub
End Class
I did manage to get it working via ApplicationEventHandler.
FYI Here's some code (in VB):-
Public Class EventHandlers
Inherits ApplicationEventHandler
Protected Overrides Sub ApplicationStarted(umbracoApplication As UmbracoApplicationBase, applicationContext As ApplicationContext)
MyBase.ApplicationStarted(umbracoApplication, applicationContext)
WriteLog("ADDING HANDLER")
AddHandler UmbracoApplicationBase.ApplicationInit, AddressOf Application_Init
End Sub
Public Sub Application_Error(sender As Object, e As EventArgs)
Dim oCTX As HttpContext = HttpContext.Current
Dim oExc As Exception = oCTX.Server.GetLastError
Dim oStk As New StackTrace(oExc, True)
Dim sError As String = ""
sError += "URL:" & oCTX.Request.Url.AbsoluteUri.ToString & vbCrLf
sError += "Message:" & oExc.Message.ToString & vbCrLf
sError += "Error:" & oExc.InnerException.Message.ToString & vbCrLf
sError += "Source:" & oExc.InnerException.Source.ToString & vbCrLf
sError += "Stack Trace:" & oExc.InnerException.StackTrace.ToString & vbCrLf
sError += "Target Site:" & oExc.TargetSite.ToString & vbCrLf
sError += "Exception Type:" & oExc.InnerException.GetType.ToString & vbCrLf
sError += vbCrLf & vbCrLf
WriteLog("ERROR - " & vbCrLf & sError)
HttpContext.Current.ClearError()
oCTX.Response.Redirect("~/500.html", False)
End Sub
Public Sub Application_Init(sender As Object, e As EventArgs)
WriteLog("APPLICATION INIT")
Dim oApp As HttpApplication = sender
AddHandler oApp.Error, AddressOf Application_Error
End Sub
Public Sub WriteLog(text As String)
Dim sPath = "/errors.txt"
Dim oSW As New StreamWriter(HttpContext.Current.Server.MapPath(sPath), True)
oSW.WriteLine(DateTime.Now.ToString("dd/MM/yyyy - HH:mm:ss") & vbTab & text)
oSW.Close()
oSW.Dispose()
End Sub
Handling 500 Errors the correct way (6.2.x & 7.x)
Hi Guys,
I have a site that I want to trap 500 errors. (v6.2.x & 7.x)
We might all consider ourselves excellent programmers and code for every unhandled exception error, but, occasionally one slips through the net.
So, this is what I'm trying to achieve:
I've trawled this forum and elsewhere for an answer. Seems like a lot of dead end pointers and no real solution.
Ive made a start on this code by creating a class that inherits ApplicationEventHandler I've overridden the ApplicationStarted medth (which fires when the application is started) but, the umbracoApplication.Error event doesn't seem to fire.
Here's my code:-
Namespace Umbraco.Extensions.EventHandlers Public Class RegisterEvents Inherits ApplicationEventHandler
End Namespace
Any pointers would be greatly appreciated.
Any one got any ideas?
Hi Mark
We just handle it in the web.config as a 500 error is essentially an application crash / failed code.
Add this to your customErrors section inside system.web
Regards
Martin
A helpful extra, setup your log4net.config to email you errors when you deploy to live.
Hi Martin,
Thanks for the info.
I did manage to get it working via ApplicationEventHandler.
FYI Here's some code (in VB):-
Public Class EventHandlers Inherits ApplicationEventHandler
End Class
Nice....but the log4net library and web.config does that all for you, so probably a bit of an overkill. But each to their own.
Often there's no right or wrong way...just lots of ways! ;-)
Martin
is working on a reply...