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)
-
0
Hi,
You can try adding breakpoints for debugging.
Has the
GlobalExceptionFilter
been executed? -
0
Yes I checked and the exception code is being executed ..
-
0
Could you share an example project with me? I will check it. my email is shiwei.liang@volosoft.com thanks.
-
0
Sure, I will share with you on Monday.
-
0
okay
-
0
I have sent email.
-
0
Hi,
I did not receive the email
-
0
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
-
0
Its working now. Thank You