Copied to clipboard

Flag this post as spam?

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


  • René 1 post 72 karma points
    Apr 30, 2018 @ 09:32
    René
    1

    Global Exception Handling

    Hi,

    i'm trying to fetch all unhandled exceptions globally to log them. I tried the following two approaches. Both approaches do not work as expected. Maybe someone can give me hint.

    1. Implemented IApplicationEventHandler and adding an EventHandler in the OnApplicationStarted method.

      public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
      {
          AreaRegistration.RegisterAllAreas();
          FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
          BundleConfig.RegisterBundles(BundleTable.Bundles);
          umbracoApplication.Error += UmbracoApplication_Error;
      }
      
      
      private void UmbracoApplication_Error(object sender, System.EventArgs e)
      {
      
      
      }
      

    But the UmbracoApplication_Error method is never called.

    1. Created a custom global.cs file where i provide a new implementation of the Appliation_Error methods.

      <%@ Application Inherits="Website.CustomGlobal" Language="C#" %>

    public class CustomGlobal : UmbracoApplication

        protected new void Application_Error(object sender, System.EventArgs e)
        {
            base.Application_Error(sender, e);
    
            var exception = Server.GetLastError();
    
            if (exception != null)
            {
                var logger = (ILogger<ApplicationEventHandler>)DependencyResolver.Current.GetService(typeof(ILogger<ApplicationEventHandler>));
                Task.Run(async  () =>
                {
                    await logger.LogHttpException(exception, Context);
                }).Wait();
    
                Server.ClearError();
            }
        }
    }
    

    This method is called allways 2x for each exception.

Please Sign in or register to post replies

Write your reply to:

Draft