From daf70c52894115c79b123cc1b1ea47026805c135 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 8 Apr 2021 11:41:00 +0800 Subject: [PATCH] Add exception to audit log if it not record. --- .../AspNetCore/Auditing/AbpAuditingMiddleware.cs | 11 +++++++++-- .../Mvc/Auditing/AuditTestController.cs | 8 ++++++++ .../Mvc/Auditing/AuditTestController_Tests.cs | 15 +++++++++++++++ .../Mvc/Auditing/AuditTestPage.cshtml.cs | 5 +++++ .../Mvc/Auditing/AuditTestPage_Tests.cs | 15 +++++++++++++++ 5 files changed, 52 insertions(+), 2 deletions(-) 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 99c9ccb2d5..72e5d36b3f 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 @@ -45,19 +45,26 @@ namespace Volo.Abp.AspNetCore.Auditing var hasError = false; using (var saveHandle = _auditingManager.BeginScope()) { + Debug.Assert(_auditingManager.Current != null); + try { await next(context); - Debug.Assert(_auditingManager.Current != null); if (_auditingManager.Current.Log.Exceptions.Any()) { hasError = true; } } - catch (Exception) + catch (Exception ex) { hasError = true; + + if (!_auditingManager.Current.Log.Exceptions.Contains(ex)) + { + _auditingManager.Current.Log.Exceptions.Add(ex); + } + throw; } finally diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController.cs index 94e57403f5..e67bbcb1ae 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController.cs @@ -31,10 +31,18 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing { throw new UserFriendlyException("Exception occurred!"); } + [Route("audit-fail-object")] public object AuditFailForGetRequestsReturningObject() { throw new UserFriendlyException("Exception occurred!"); } + + [HttpGet] + [Route("audit-activate-failed")] + public IActionResult AuditActivateFailed([FromServices] AbpAuditingOptions options) + { + return Ok(); + } } } diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController_Tests.cs index 9d3da08095..b76e71de52 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController_Tests.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestController_Tests.cs @@ -74,5 +74,20 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing await _auditingStore.Received().SaveAsync(Arg.Any()); } + + [Fact] + public async Task Should_Trigger_Middleware_And_AuditLog_Exception_When_Activate_Controller_Failed() + { + _options.IsEnabledForGetRequests = true; + _options.AlwaysLogOnException = true; + + try + { + await GetResponseAsync("api/audit-test/audit-activate-failed", System.Net.HttpStatusCode.InternalServerError); + } + catch { } + + await _auditingStore.Received().SaveAsync(Arg.Is(x => x.Exceptions.Any())); + } } } diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage.cshtml.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage.cshtml.cs index 7dc1967a2e..10e28a4bb1 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage.cshtml.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage.cshtml.cs @@ -34,5 +34,10 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing { throw new UserFriendlyException("Exception occurred!"); } + + public IActionResult OnGetAuditActivateFailed([FromServices] AbpAuditingOptions options) + { + return new OkResult(); + } } } diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage_Tests.cs index 311abe5aa5..24d7a58dd3 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage_Tests.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Auditing/AuditTestPage_Tests.cs @@ -73,5 +73,20 @@ namespace Volo.Abp.AspNetCore.Mvc.Auditing await _auditingStore.Received().SaveAsync(Arg.Any()); } + + [Fact] + public async Task Should_Trigger_Middleware_And_AuditLog_Exception_When_Activate_Page_Failed() + { + _options.IsEnabledForGetRequests = true; + _options.AlwaysLogOnException = true; + + try + { + await GetResponseAsync("/Auditing/AuditTestPage?handler=AuditActivateFailed", System.Net.HttpStatusCode.InternalServerError); + } + catch { } + + await _auditingStore.Received().SaveAsync(Arg.Is(x => x.Exceptions.Any())); + } } }