mirror of https://github.com/abpframework/abp.git
9 changed files with 153 additions and 9 deletions
@ -0,0 +1,72 @@ |
|||||
|
using System; |
||||
|
using System.Diagnostics; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Mvc.Abstractions; |
||||
|
using Microsoft.AspNetCore.Mvc.Filters; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.Aspects; |
||||
|
using Volo.Abp.Auditing; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.Auditing |
||||
|
{ |
||||
|
public class AbpAuditActionFilter : IAsyncActionFilter, ITransientDependency |
||||
|
{ |
||||
|
protected AuditingOptions Options { get; } |
||||
|
private readonly IAuditingHelper _auditingHelper; |
||||
|
|
||||
|
public AbpAuditActionFilter(IOptions<AuditingOptions> options, IAuditingHelper auditingHelper) |
||||
|
{ |
||||
|
Options = options.Value; |
||||
|
_auditingHelper = auditingHelper; |
||||
|
} |
||||
|
|
||||
|
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) |
||||
|
{ |
||||
|
if (!ShouldSaveAudit(context)) |
||||
|
{ |
||||
|
await next(); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
using (AbpCrossCuttingConcerns.Applying(context.Controller, AbpCrossCuttingConcerns.Auditing)) |
||||
|
{ |
||||
|
var auditInfo = _auditingHelper.CreateAuditInfo( |
||||
|
context.ActionDescriptor.AsControllerActionDescriptor().ControllerTypeInfo.AsType(), |
||||
|
context.ActionDescriptor.AsControllerActionDescriptor().MethodInfo, |
||||
|
context.ActionArguments |
||||
|
); |
||||
|
|
||||
|
var stopwatch = Stopwatch.StartNew(); |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
var result = await next(); |
||||
|
if (result.Exception != null && !result.ExceptionHandled) |
||||
|
{ |
||||
|
auditInfo.Exception = result.Exception; |
||||
|
} |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
auditInfo.Exception = ex; |
||||
|
throw; |
||||
|
} |
||||
|
finally |
||||
|
{ |
||||
|
stopwatch.Stop(); |
||||
|
auditInfo.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds); |
||||
|
auditInfo.ServiceName = "AbpAuditActionFilter->" + auditInfo.ServiceName; |
||||
|
await _auditingHelper.SaveAsync(auditInfo); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private bool ShouldSaveAudit(ActionExecutingContext actionContext) |
||||
|
{ |
||||
|
return Options.IsEnabled && |
||||
|
actionContext.ActionDescriptor.IsControllerAction() && |
||||
|
_auditingHelper.ShouldSaveAudit(actionContext.ActionDescriptor.GetMethodInfo(), true); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,59 @@ |
|||||
|
using System; |
||||
|
using Castle.Core.Logging; |
||||
|
using Microsoft.AspNetCore.Http; |
||||
|
using Volo.Abp.Auditing; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.Auditing |
||||
|
{ |
||||
|
public class HttpContextClientInfoProvider : IClientInfoProvider |
||||
|
{ |
||||
|
public string BrowserInfo => GetBrowserInfo(); |
||||
|
|
||||
|
public string ClientIpAddress => GetClientIpAddress(); |
||||
|
|
||||
|
public string ComputerName => GetComputerName(); |
||||
|
|
||||
|
public ILogger Logger { get; set; } |
||||
|
|
||||
|
private readonly IHttpContextAccessor _httpContextAccessor; |
||||
|
|
||||
|
private readonly HttpContext _httpContext; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Creates a new <see cref="HttpContextClientInfoProvider"/>.
|
||||
|
/// </summary>
|
||||
|
public HttpContextClientInfoProvider(IHttpContextAccessor httpContextAccessor) |
||||
|
{ |
||||
|
_httpContextAccessor = httpContextAccessor; |
||||
|
_httpContext = httpContextAccessor.HttpContext; |
||||
|
|
||||
|
Logger = NullLogger.Instance; |
||||
|
} |
||||
|
|
||||
|
protected virtual string GetBrowserInfo() |
||||
|
{ |
||||
|
var httpContext = _httpContextAccessor.HttpContext ?? _httpContext; |
||||
|
return httpContext?.Request?.Headers?["User-Agent"]; |
||||
|
} |
||||
|
|
||||
|
protected virtual string GetClientIpAddress() |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
var httpContext = _httpContextAccessor.HttpContext ?? _httpContext; |
||||
|
return httpContext?.Connection?.RemoteIpAddress?.ToString(); |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
Logger.Warn(ex.ToString()); |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
protected virtual string GetComputerName() |
||||
|
{ |
||||
|
return null; //TODO: Implement!
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue