Ends in:
1 DAY
6 HRS
40 MIN
17 SEC
Ends in:
1 D
6 H
40 M
17 S
Open Closed

Azure service bus on Abp Exception Filter #7307


User avatar
0
imranStem created

I have Microservice architecture. I have added Custom exception filter in NxP.Shared.Hosting.Microservices.

namespace NxP.Shared.Hosting.Microservices
{
    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(AbpExceptionFilter), typeof(IAsyncExceptionFilter))]
    public class GlobalExceptionFilter : AbpExceptionFilter, ITransientDependency
    {
        private readonly IDistributedEventBus _distributedEventBus;
        private readonly ILogger< GlobalExceptionFilter > _logger;

        public GlobalExceptionFilter(IDistributedEventBus distributedEventBus, ILogger< GlobalExceptionFilter > logger)
        {
            _distributedEventBus = distributedEventBus;
            _logger = logger;
        }

        public override async  Task OnExceptionAsync(ExceptionContext context)
        {
            var exception = context.Exception;
            var actionDescriptor = context.ActionDescriptor;
            var exceptionLogEto = new ExceptionLogEto
            {
                Message = exception.Message,
                Type = exception.GetType().Name,
                StackTrace = exception.InnerException?.Message ?? exception.StackTrace,
                Source = exception.Source,
                ControllerName = actionDescriptor.RouteValues["controller"],
                ActionName = actionDescriptor.RouteValues["action"],
            };

            await _distributedEventBus.PublishAsync(exceptionLogEto);
            _logger.LogInformation("Exception handled by GlobalExceptionFilter.");

            //await base.OnExceptionAsync(context);
            // throw exception;
            //context.ExceptionHandled = true;
        }
    }
}

I have added filters in services in NxPSharedHostingMicroservicesModule

 context.Services.AddTransient< GlobalExceptionFilter >();

 context.Services.AddControllers(options =>
 {
     options.Filters.AddService< GlobalExceptionFilter >();
 }).AddControllersAsServices();

I have handler in LoggingService to receive exception data and storing in database, but the event is not receiving in LoggingService. There is no exception and event data is not publishing. I have also checked the sample console application.

Handler in logging microservice.

 public class ExceptionLogHandler : IDistributedEventHandler< ExceptionLogEto >, ITransientDependency
 {
     private readonly ILogger< ExceptionLogHandler > _logger;
     private readonly IExceptionLogRepository _exceptionLogRepository;

     public ExceptionLogHandler(ILogger< ExceptionLogHandler > logger
         , IExceptionLogRepository exceptionLogRepository
         )
     {
         _logger = logger;
         _exceptionLogRepository = exceptionLogRepository;
         _logger.LogInformation("ExceptionLogHandler initialized.");
     }

     [UnitOfWork]
     public async Task HandleEventAsync(ExceptionLogEto eventData)
     {
         _logger.LogInformation("Handling exception log...");

         ExceptionLog exceptionLog = new ExceptionLog(
             Guid.NewGuid(),
             eventData.Message,
             eventData.Type,
             eventData.StackTrace,
             eventData.Source,
             eventData.ControllerName,
             eventData.ActionName,
             DateTime.Now
             );
         _logger.LogInformation(Newtonsoft.Json.JsonConvert.SerializeObject(exceptionLog));
         await _exceptionLogRepository.InsertAsync(exceptionLog);
     }
 }
  • ABP Framework version: v8.1.3
  • UI Type: Angular
  • Database System: EF Core (SQL Server)
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes
  • Exception message and full stack trace:
  • Steps to reproduce the issue:

9 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can try adding breakpoints for debugging.

    Has the GlobalExceptionFilter been executed?

  • User Avatar
    0
    imranStem created

    Yes I checked and the exception code is being executed ..

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Could you share an example project with me? I will check it. my email is shiwei.liang@volosoft.com thanks.

  • User Avatar
    0
    imranStem created

    Sure, I will share with you on Monday.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    okay

  • User Avatar
    0
    imranStem created

    I have sent email.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I did not receive the email

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    HI,

    Try

    await _distributedEventBus.PublishAsync(exceptionLogEto, onUnitOfWorkComplete: false);

    The transaction was rolled back, so no events were published

    See https://docs.abp.io/en/abp/latest/Distributed-Event-Bus#transaction-and-exception-handling

  • User Avatar
    0
    imranStem created

    Its working now. Thank You

Made with ❤️ on ABP v9.1.0-preview. Updated on December 05, 2024, 12:19