Copied to clipboard

Flag this post as spam?

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


  • Suresh 2 posts 72 karma points
    Jul 10, 2023 @ 17:21
    Suresh
    0

    Global exception handling and logging in umbraco 10

    Hi All ,

    I have implemented exception handler in startup to catch exceptions globally, however exceptions throw from Notification handlers are not logged.

    Any assistance is greatly appreciated.

    tried below two options

    1. Using Exception handler: registered UseExceptionHandler middleware early in the pipeline as below.

              app.UseExceptionHandler( builder => builder.Run(async context =>
          {var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>();
      
      
      
          var exception = exceptionHandlerPathFeature?.Error;
          logger.LogError(exception, exception?.Message);
          await context.Response.WriteAsJsonAsync(
              new ExceptionResponse()
              {
                  Message = exception?.Message, 
              });
      }));
      
      1. Exception filters: added global exception filter

    But seems both are not working.

  • Ben 112 posts 398 karma points
    Jan 03, 2024 @ 21:11
    Ben
    0

    I found the information here to be helpful: https://enlear.academy/global-exception-handling-in-net-6-16908fc8dc28

    I created a custom class in a new Middleware folder called Startup and did this:

    namespace Umbraco_Project.Middleware;
    
    public static class Startup
    {
        public static IApplicationBuilder UseGlobalExceptionHandler(this IApplicationBuilder app) => app.UseMiddleware<ExceptionMiddlewareV2>();
    }
    

    Then I injected this in the main Startup.cs file instead of the Program.cs like this:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseGlobalExceptionHandler();
        ...
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft