Copied to clipboard

Flag this post as spam?

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


  • Chris Perks 32 posts 114 karma points
    Dec 23, 2014 @ 11:04
    Chris Perks
    0

    Catching and logging UmbracoApi errors

    Hi,

    I have two levels of logging set up in my Umbraco 7.1.4 application:

    //1

    public class MainEventsHandler : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            umbracoApplication.Error += UmbracoApplication_Error;
        }
       
        void UmbracoApplication_Error(object sender, EventArgs e)
        {
            //do something
        }
    }


    //2

    public class EmailErrorGlobal : UmbracoApplication
    {
        protected override void OnApplicationError(object sender, EventArgs e)
        {
            //do something
        }
    }

     

    Neither cases will trigger when there's an error generated in an UmbracoApi handler, such as

    http://localhost/umbraco/backoffice/UmbracoApi/Content/PostSave

    Forcing the application to error, by doing something silly such as:

    public class CrashOnStartup : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
              throw new Exception();
        }
    }

    works as expected.

    Can anyone please help me understand what is going on?

     

    Thanks,

    Chris.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Dec 23, 2014 @ 11:06
    Jan Skovgaard
    0

    Hi Chris

    Perhaps this post in last years version of the umbraco advent calendar can help? http://24days.in/umbraco/2013/log4net-in-umbraco/

    /Jan

  • Chris Perks 32 posts 114 karma points
    Dec 23, 2014 @ 11:09
    Chris Perks
    0

    Cool! Will give it a read and let you know. Thanks.

  • Chris Perks 32 posts 114 karma points
    Dec 23, 2014 @ 11:52
    Chris Perks
    0

    Based on the article, I tried the custom Error.aspx Web.Config redirect - which doesn't kick in for UmbracoApi errors either.

    I suspect that this is related to Web Api rather than anything to do with Umbraco - for instance I can see that Umbraco is rethrowing the exceptions correctly, I'm getting a 500 response back in my browser. See more here : http://stackoverflow.com/questions/16028919/catch-all-unhandled-exceptions-in-asp-net-web-api

    I'm going to continue reading, will post any solutions I find here.

     

    Thanks,

    Chris.

     

  • Chris Perks 32 posts 114 karma points
    Dec 23, 2014 @ 12:26
    Chris Perks
    0

    Web API 2.1 (System.Web.Http v 5.1.0) allows catching and logging of Web API exceptions (see this answer : http://stackoverflow.com/a/21382651)

    However, until then, I think I'll have to:

    • Use Try/Catch in my own API Controllers to log anything unhandled, and then re-throw
    • Rely on Umbraco logging deeper down in the stack, at a ContentService level, for instance, and hope those errors are logged. Also hope any Core controllers are kept slim, delegating their work to services, so there's less chance of unhandled exceptions occuring near the top.

     

     

Please Sign in or register to post replies

Write your reply to:

Draft