From dc4d41dca15621f4ab1cfb66126938ef3145372b Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 24 Feb 2022 13:16:35 +0800 Subject: [PATCH] Handle possible exceptions in SaveChangesAsync method. --- .../SignalR/Auditing/AbpAuditHubFilter.cs | 12 +++++++++- .../Auditing/AbpAuditingMiddleware.cs | 12 +++++++++- .../Volo/Abp/Auditing/AuditingInterceptor.cs | 22 +++++++++++++++++-- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Auditing/AbpAuditHubFilter.cs b/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Auditing/AbpAuditHubFilter.cs index d622ba535a7..fd5567396bf 100644 --- a/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Auditing/AbpAuditHubFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Auditing/AbpAuditHubFilter.cs @@ -55,7 +55,17 @@ public virtual async ValueTask InvokeMethodAsync(HubInvocationContext in var unitOfWorkManager = invocationContext.ServiceProvider.GetRequiredService(); if (unitOfWorkManager.Current != null) { - await unitOfWorkManager.Current.SaveChangesAsync(); + try + { + await unitOfWorkManager.Current.SaveChangesAsync(); + } + catch (Exception ex) + { + if (!auditingManager.Current.Log.Exceptions.Contains(ex)) + { + auditingManager.Current.Log.Exceptions.Add(ex); + } + } } await saveHandle.SaveAsync(); diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs index 5d0462bc5ff..4ff49012cf6 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs @@ -73,7 +73,17 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next) { if (UnitOfWorkManager.Current != null) { - await UnitOfWorkManager.Current.SaveChangesAsync(); + try + { + await UnitOfWorkManager.Current.SaveChangesAsync(); + } + catch (Exception ex) + { + if (!_auditingManager.Current.Log.Exceptions.Contains(ex)) + { + _auditingManager.Current.Log.Exceptions.Add(ex); + } + } } await saveHandle.SaveAsync(); diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingInterceptor.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingInterceptor.cs index 4f4d3727bbf..5521c28d1ee 100644 --- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingInterceptor.cs +++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingInterceptor.cs @@ -7,6 +7,7 @@ using Volo.Abp.Aspects; using Volo.Abp.DependencyInjection; using Volo.Abp.DynamicProxy; +using Volo.Abp.Uow; using Volo.Abp.Users; namespace Volo.Abp.Auditing; @@ -41,7 +42,8 @@ public override async Task InterceptAsync(IAbpMethodInvocation invocation) else { var currentUser = serviceScope.ServiceProvider.GetRequiredService(); - await ProcessWithNewAuditingScopeAsync(invocation, auditingOptions, currentUser, auditingManager, auditingHelper); + var unitOfWorkManager = serviceScope.ServiceProvider.GetRequiredService(); + await ProcessWithNewAuditingScopeAsync(invocation, auditingOptions, currentUser, auditingManager, auditingHelper, unitOfWorkManager); } } } @@ -105,7 +107,8 @@ private async Task ProcessWithNewAuditingScopeAsync( AbpAuditingOptions options, ICurrentUser currentUser, IAuditingManager auditingManager, - IAuditingHelper auditingHelper) + IAuditingHelper auditingHelper, + IUnitOfWorkManager unitOfWorkManager) { var hasError = false; using (var saveHandle = auditingManager.BeginScope()) @@ -129,6 +132,21 @@ private async Task ProcessWithNewAuditingScopeAsync( { if (ShouldWriteAuditLog(invocation, options, currentUser, hasError)) { + if (unitOfWorkManager.Current != null) + { + try + { + await unitOfWorkManager.Current.SaveChangesAsync(); + } + catch (Exception ex) + { + if (!auditingManager.Current.Log.Exceptions.Contains(ex)) + { + auditingManager.Current.Log.Exceptions.Add(ex); + } + } + } + await saveHandle.SaveAsync(); } }