Copied to clipboard

Flag this post as spam?

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


  • Scott Ritshie 35 posts 100 karma points
    Aug 06, 2020 @ 19:16
    Scott Ritshie
    0

    Registering custom error 500 handling (with e-mail send) on startup

    Hey there,

    I've created a method to send the StackTrace exception to me in an e-mail and want to ensure it's called upon when an error 500 gets fired. I have a new Global class implemented, but am unsure of how to get it called by default. Any thoughts?

    This is all in Global.ascx.cs. NOTE that the ApplicationComposer class inheritance isn't rendering properly here, so FYI, ComponentComposer's T<> is ApplicationComponent and uses IUserComposer.

    public class ApplicationComposer : ComponentComposer
      {
        public override void Compose(Composition composition)
        {
          composition.SetDefaultRenderMvcController(typeof(Controllers.MasterController));
    
          if (ServicePointManager.SecurityProtocol.HasFlag(SecurityProtocolType.Tls12) == false)
          {
            ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls12;
          }
    
          base.Compose(composition);
        }    
      }
    
      public class Global : UmbracoApplication
      {
        private Utilities _utilities = new Utilities();
    
        protected override void OnApplicationError(object sender, EventArgs e)
        {
          Exception ex = HttpContext.Current.Server.GetLastError();
          _utilities.SendErrorEmail(ex);
        }
      }
    
      public class ApplicationComponent : IComponent
      {
        public void Initialize()
        {
        }
    
        public void Terminate()
        }
        }
      }
    

    Thanks for any help!

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Aug 09, 2020 @ 15:23
    Huw Reddick
    0

    I've not done this in umbraco yet, but have setup custom error pages in MVC, it is not simple I'm afraid.

    I will dig out some instructions for you, but you will need to add some stuff to the web.config and use a controller/method to send the email and render your custom 500 page.

    You will also need to create some .hml and .aspx pages as fall back incase umbraco or your code causes an error otherwise you will probably get an err too many redirects from your error page.

  • Scott Ritshie 35 posts 100 karma points
    Aug 11, 2020 @ 19:25
    Scott Ritshie
    0

    Thanks, Huw. Yes -- it's hitting the custom error controller that I'm having trouble with... i.e. where and how to register that controller as the go-to upon 500 errors.

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Aug 13, 2020 @ 16:04
    Huw Reddick
    0

    Apologies for delay, been a bit busy with work. I will dig out the code I have over the weekend and post it.

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Aug 15, 2020 @ 20:34
    Huw Reddick
    0

    These are the instructions I followed. It works for a standard .Net mvc sites, I will give it a try out in Umbraco next week. I know for sure there a couple of umbraco config settings to change to use custom 404, so not sure if the same is tru for custom 500

    Asp.Net MVC custom error handling

  • Scott Ritshie 35 posts 100 karma points
    Aug 17, 2020 @ 00:38
    Scott Ritshie
    0

    Huw,

    Thanks for taking the time to find this.

    I have this already setup in web.config, so maybe I wasn't clear. I'm unclear as to how to register a default action that handles errors. Outside of Umbraco, this is a piece of cake in Global.ascx.cs, but Umbraco 8 needs its own method that I haven't been able to find. That "override" method should call upon a public email sending method I've created.

    Does that make sense?

    Will keep searching. Thanks again.

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Aug 27, 2020 @ 12:48
    Huw Reddick
    0

    sorry for long delay, I've been a bit busy with work.

    I have Umbraco displaying a standard Umbraco page in response to a HttpUnhandledException

    All I did for that was to create a page called error500 and point web config at that

    <customErrors mode="On" defaultRedirect="/error404" >
      <error statusCode="500" redirect="/error500"/>
    </customErrors>
    

    you should then be able to call a controller action from that which sends an email

    I throw an unhandled error in one of my controllers and it directed me to my custom error500 page

Please Sign in or register to post replies

Write your reply to:

Draft