Implemented PreContribute and PostContribute for audit contributors. Added HttpStatusCode to the audit log info.

pull/395/head
Halil ibrahim Kalkan 7 years ago
parent 1fec81094e
commit e770e4ff7e

@ -1,5 +1,4 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
@ -18,12 +17,12 @@ namespace Volo.Abp.AspNetCore.Auditing
Logger = NullLogger<AspNetCoreAuditLogContributor>.Instance;
}
public override Task ContributeAsync(AuditLogContributionContext context)
public override void PreContribute(AuditLogContributionContext context)
{
var httpContext = context.ServiceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext;
if (httpContext == null)
{
return Task.CompletedTask;
return;
}
if (context.AuditInfo.HttpMethod == null)
@ -47,8 +46,20 @@ namespace Volo.Abp.AspNetCore.Auditing
}
//TODO: context.AuditInfo.ClientName
}
return Task.CompletedTask;
public override void PostContribute(AuditLogContributionContext context)
{
var httpContext = context.ServiceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext;
if (httpContext == null)
{
return;
}
if (context.AuditInfo.HttpStatusCode == null)
{
context.AuditInfo.HttpStatusCode = httpContext.Response.StatusCode;
}
}
protected virtual string GetBrowserInfo(HttpContext httpContext)

@ -1,9 +1,15 @@
using System.Threading.Tasks;
namespace Volo.Abp.Auditing
namespace Volo.Abp.Auditing
{
public abstract class AuditLogContributor
{
public abstract Task ContributeAsync(AuditLogContributionContext context);
public virtual void PreContribute(AuditLogContributionContext context)
{
}
public virtual void PostContribute(AuditLogContributionContext context)
{
}
}
}

@ -30,6 +30,8 @@ namespace Volo.Abp.Auditing
public string HttpMethod { get; set; }
public int? HttpStatusCode { get; set; }
public string Url { get; set; }
public List<AuditLogActionInfo> Actions { get; set; }
@ -55,7 +57,7 @@ namespace Volo.Abp.Auditing
{
var sb = new StringBuilder();
sb.AppendLine($"AUDIT LOG: [{HttpMethod ?? "?"}] {Url}");
sb.AppendLine($"AUDIT LOG: [{HttpStatusCode?.ToString() ?? "---"}: {(HttpMethod ?? "-------").PadRight(7)}] {Url}");
sb.AppendLine($"- UserId : {UserId}");
sb.AppendLine($"- ClientIpAddress : {ClientIpAddress}");
sb.AppendLine($"- ExecutionDuration : {ExecutionDuration}");

@ -89,7 +89,7 @@ namespace Volo.Abp.Auditing
ExecutionTime = Clock.Now
};
ExecuteContributors(auditInfo);
ExecutePreContributors(auditInfo);
return auditInfo;
}
@ -116,7 +116,7 @@ namespace Volo.Abp.Auditing
return actionInfo;
}
protected virtual void ExecuteContributors(AuditLogInfo auditLogInfo)
protected virtual void ExecutePreContributors(AuditLogInfo auditLogInfo)
{
using (var scope = ServiceProvider.CreateScope())
{
@ -126,7 +126,7 @@ namespace Volo.Abp.Auditing
{
try
{
contributor.ContributeAsync(context);
contributor.PreContribute(context);
}
catch (Exception ex)
{

@ -1,6 +1,10 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Threading;
@ -10,6 +14,9 @@ namespace Volo.Abp.Auditing
{
private const string AmbientContextKey = "Volo.Abp.Auditing.IAuditLogScope";
protected IServiceProvider ServiceProvider { get; }
protected AbpAuditingOptions Options { get; }
protected ILogger<AuditingManager> Logger { get; set; }
private readonly IAmbientScopeProvider<IAuditLogScope> _ambientScopeProvider;
private readonly IAuditingHelper _auditingHelper;
private readonly IAuditingStore _auditingStore;
@ -17,8 +24,14 @@ namespace Volo.Abp.Auditing
public AuditingManager(
IAmbientScopeProvider<IAuditLogScope> ambientScopeProvider,
IAuditingHelper auditingHelper,
IAuditingStore auditingStore)
IAuditingStore auditingStore,
IServiceProvider serviceProvider,
IOptions<AbpAuditingOptions> options)
{
ServiceProvider = serviceProvider;
Options = options.Value;
Logger = NullLogger<AuditingManager>.Instance;
_ambientScopeProvider = ambientScopeProvider;
_auditingHelper = auditingHelper;
_auditingStore = auditingStore;
@ -35,45 +48,79 @@ namespace Volo.Abp.Auditing
Debug.Assert(Current != null, "Current != null");
var stopWatch = Stopwatch.StartNew();
return new DisposableSaveHandle(this, ambientScope, Current.Log, Stopwatch.StartNew());
}
return new DisposableSaveHandle(_auditingStore, ambientScope, Current.Log, stopWatch);
protected virtual void ExecutePostContributors(AuditLogInfo auditLogInfo)
{
using (var scope = ServiceProvider.CreateScope())
{
var context = new AuditLogContributionContext(scope.ServiceProvider, auditLogInfo);
foreach (var contributor in Options.Contributors)
{
try
{
contributor.PostContribute(context);
}
catch (Exception ex)
{
Logger.LogException(ex, LogLevel.Warning);
}
}
}
}
private class DisposableSaveHandle : IAuditLogSaveHandle
protected virtual void BeforeSave(DisposableSaveHandle saveHandle)
{
private readonly IAuditingStore _auditingStore;
private readonly IDisposable _scope;
private readonly AuditLogInfo _auditLog;
private readonly Stopwatch _stopWatch;
saveHandle.StopWatch.Stop();
saveHandle.AuditLog.ExecutionDuration = Convert.ToInt32(saveHandle.StopWatch.Elapsed.TotalMilliseconds);
ExecutePostContributors(saveHandle.AuditLog);
}
protected virtual async Task SaveAsync(DisposableSaveHandle saveHandle)
{
BeforeSave(saveHandle);
await _auditingStore.SaveAsync(saveHandle.AuditLog);
}
protected virtual void Save(DisposableSaveHandle saveHandle)
{
BeforeSave(saveHandle);
_auditingStore.Save(saveHandle.AuditLog);
}
protected class DisposableSaveHandle : IAuditLogSaveHandle
{
public AuditLogInfo AuditLog { get; }
public Stopwatch StopWatch { get; }
private readonly AuditingManager _auditingManager;
private readonly IDisposable _scope;
private bool _saved;
public DisposableSaveHandle(IAuditingStore auditingStore, IDisposable scope, AuditLogInfo auditLog, Stopwatch stopWatch)
public DisposableSaveHandle(
AuditingManager auditingManager,
IDisposable scope,
AuditLogInfo auditLog,
Stopwatch stopWatch)
{
_auditingStore = auditingStore;
_auditingManager = auditingManager;
_scope = scope;
_auditLog = auditLog;
_stopWatch = stopWatch;
}
private void BeforeSave()
{
_stopWatch.Stop();
_saved = true;
_auditLog.ExecutionDuration = Convert.ToInt32(_stopWatch.Elapsed.TotalMilliseconds);
AuditLog = auditLog;
StopWatch = stopWatch;
}
public async Task SaveAsync()
{
BeforeSave();
await _auditingStore.SaveAsync(_auditLog);
_saved = true;
await _auditingManager.SaveAsync(this);
}
public void Save()
{
BeforeSave();
_auditingStore.Save(_auditLog);
_saved = true;
_auditingManager.Save(this);
}
public void Dispose()

Loading…
Cancel
Save