Browse Source

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

pull/395/head
Halil ibrahim Kalkan 8 years ago
parent
commit
e770e4ff7e
  1. 19
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AspNetCoreAuditLogContributor.cs
  2. 14
      framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditLogContributor.cs
  3. 4
      framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditLogInfo.cs
  4. 6
      framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingHelper.cs
  5. 93
      framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs

19
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AspNetCoreAuditLogContributor.cs

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

14
framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditLogContributor.cs

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

4
framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditLogInfo.cs

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

6
framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingHelper.cs

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

93
framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingManager.cs

@ -1,6 +1,10 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Threading.Tasks; 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.DependencyInjection;
using Volo.Abp.Threading; using Volo.Abp.Threading;
@ -10,6 +14,9 @@ namespace Volo.Abp.Auditing
{ {
private const string AmbientContextKey = "Volo.Abp.Auditing.IAuditLogScope"; 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 IAmbientScopeProvider<IAuditLogScope> _ambientScopeProvider;
private readonly IAuditingHelper _auditingHelper; private readonly IAuditingHelper _auditingHelper;
private readonly IAuditingStore _auditingStore; private readonly IAuditingStore _auditingStore;
@ -17,8 +24,14 @@ namespace Volo.Abp.Auditing
public AuditingManager( public AuditingManager(
IAmbientScopeProvider<IAuditLogScope> ambientScopeProvider, IAmbientScopeProvider<IAuditLogScope> ambientScopeProvider,
IAuditingHelper auditingHelper, IAuditingHelper auditingHelper,
IAuditingStore auditingStore) IAuditingStore auditingStore,
IServiceProvider serviceProvider,
IOptions<AbpAuditingOptions> options)
{ {
ServiceProvider = serviceProvider;
Options = options.Value;
Logger = NullLogger<AuditingManager>.Instance;
_ambientScopeProvider = ambientScopeProvider; _ambientScopeProvider = ambientScopeProvider;
_auditingHelper = auditingHelper; _auditingHelper = auditingHelper;
_auditingStore = auditingStore; _auditingStore = auditingStore;
@ -35,45 +48,79 @@ namespace Volo.Abp.Auditing
Debug.Assert(Current != null, "Current != null"); 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; saveHandle.StopWatch.Stop();
private readonly IDisposable _scope; saveHandle.AuditLog.ExecutionDuration = Convert.ToInt32(saveHandle.StopWatch.Elapsed.TotalMilliseconds);
private readonly AuditLogInfo _auditLog; ExecutePostContributors(saveHandle.AuditLog);
private readonly Stopwatch _stopWatch; }
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; 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; _scope = scope;
_auditLog = auditLog; AuditLog = auditLog;
_stopWatch = stopWatch; StopWatch = stopWatch;
}
private void BeforeSave()
{
_stopWatch.Stop();
_saved = true;
_auditLog.ExecutionDuration = Convert.ToInt32(_stopWatch.Elapsed.TotalMilliseconds);
} }
public async Task SaveAsync() public async Task SaveAsync()
{ {
BeforeSave(); _saved = true;
await _auditingStore.SaveAsync(_auditLog); await _auditingManager.SaveAsync(this);
} }
public void Save() public void Save()
{ {
BeforeSave(); _saved = true;
_auditingStore.Save(_auditLog); _auditingManager.Save(this);
} }
public void Dispose() public void Dispose()

Loading…
Cancel
Save