Merge pull request #1111 from demirmusa/dev

Fix #365 Throwing/hiding exception on audit logging should be optional
pull/1120/head
Halil İbrahim Kalkan 7 years ago committed by GitHub
commit 168fd61303
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,6 +9,12 @@ namespace Volo.Abp.Auditing
{
//TODO: Consider to add an option to disable auditing for application service methods?
/// <summary>
/// If this value is true, auditing will not throw an exceptions and it will log it when an error occurred while saving AuditLog.
/// Default: true.
/// </summary>
public bool HideErrors { get; set; }
/// <summary>
/// Default: true.
/// </summary>
@ -36,11 +42,12 @@ namespace Volo.Abp.Auditing
/// Default: false.
/// </summary>
public bool IsEnabledForGetRequests { get; set; }
public AbpAuditingOptions()
{
IsEnabled = true;
IsEnabledForAnonymousUsers = true;
HideErrors = true;
Contributors = new List<AuditLogContributor>();

@ -2,6 +2,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Volo.Abp.Auditing;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
@ -16,29 +17,42 @@ namespace Volo.Abp.AuditLogging
private readonly IAuditLogRepository _auditLogRepository;
private readonly IGuidGenerator _guidGenerator;
private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly AbpAuditingOptions Options;
public AuditingStore(
IAuditLogRepository auditLogRepository,
IGuidGenerator guidGenerator,
IUnitOfWorkManager unitOfWorkManager)
IUnitOfWorkManager unitOfWorkManager,
IOptions<AbpAuditingOptions> options)
{
_auditLogRepository = auditLogRepository;
_guidGenerator = guidGenerator;
_unitOfWorkManager = unitOfWorkManager;
Options = options.Value;
Logger = NullLogger<AuditingStore>.Instance;
}
private async Task SaveLogAsync(AuditLogInfo auditInfo)
{
using (var uow = _unitOfWorkManager.Begin(requiresNew: true))
{
var auditLog = new AuditLog(_guidGenerator, auditInfo);
await _auditLogRepository.InsertAsync(auditLog);
await uow.SaveChangesAsync();
}
}
public async Task SaveAsync(AuditLogInfo auditInfo)
{
if (!Options.HideErrors)
{
await SaveLogAsync(auditInfo);
return;
}
try
{
using (var uow = _unitOfWorkManager.Begin(requiresNew: true))
{
var auditLog = new AuditLog(_guidGenerator, auditInfo);
await _auditLogRepository.InsertAsync(auditLog);
await uow.SaveChangesAsync();
}
await SaveLogAsync(auditInfo);
}
catch (Exception ex)
{

Loading…
Cancel
Save