Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mark Slade 48 posts 109 karma points
    May 14, 2015 @ 12:07
    Mark Slade
    0

    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:

    1. A unhandled exception occurs in a piece of code that I've written.
    2. Umbraco logs the error in the normal way (in UmbracoTraceLog)
    3. My handler then emails me with the details of the exception and then
    4. 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
    

    End Namespace

    Any pointers would be greatly appreciated.

  • Mark Slade 48 posts 109 karma points
    May 22, 2015 @ 18:28
    Mark Slade
    0

    Any one got any ideas?

  • Martin Griffiths 826 posts 1269 karma points c-trib
    May 28, 2015 @ 15:55
    Martin Griffiths
    0

    Hi Mark

    We just handle it in the web.config as a 500 error is essentially an application crash / failed code.

    <error statusCode="500" redirect="/error.html" />
    

    Add this to your customErrors section inside system.web

    Regards

    Martin

  • Martin Griffiths 826 posts 1269 karma points c-trib
    May 28, 2015 @ 15:58
    Martin Griffiths
    1

    A helpful extra, setup your log4net.config to email you errors when you deploy to live.

    <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
        <to value="[email protected]" />
        <from value="[email protected]" />
        <subject value="Log4Net Error" />
        <smtpHost value="localhost" />
        <bufferSize value="512" />
        <lossy value="true" />
        <evaluator type="log4net.Core.LevelEvaluator">
          <threshold value="ERROR"/>
        </evaluator>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
        </layout>
      </appender>
  • Mark Slade 48 posts 109 karma points
    May 28, 2015 @ 16:55
    Mark Slade
    0

    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

    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
    

    End Class

  • Martin Griffiths 826 posts 1269 karma points c-trib
    May 28, 2015 @ 16:59
    Martin Griffiths
    0

    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 

Please Sign in or register to post replies

Write your reply to:

Draft