private async Task ProcessWithNewAuditingScopeAsync( IMethodInvocation invocation, AuditingOptions options, ICurrentUser currentUser, IAuditingManager auditingManager, IAuditingFactory auditingHelper) { var hasError = false; using var saveHandle = auditingManager.BeginScope(); try { await ProceedByLoggingAsync(invocation, auditingHelper, auditingManager.Current); Debug.Assert(auditingManager.Current != null); if (auditingManager.Current.Log.Exceptions.Any()) { hasError = true; } } catch (Exception) { hasError = true; throw; } finally { if (ShouldWriteAuditLog(invocation, options, currentUser, hasError)) { await saveHandle.SaveAsync(); } } }
/// <summary> /// /// </summary> /// <param name="next"></param> /// <param name="auditingManager"></param> /// <param name="options"></param> public AuditingMiddleware( RequestDelegate next, IAuditingManager auditingManager, IOptions <AuditingOptions> options) { _next = next; _auditingManager = auditingManager; Options = options.Value; }
/// <summary> /// 是否拦截 /// </summary> /// <param name="invocation"></param> /// <param name="options"></param> /// <param name="auditingHelper"></param> /// <returns></returns> protected virtual bool ShouldIntercept(IMethodInvocation invocation, AuditingOptions options, IAuditingFactory auditingHelper) { if (!options.IsEnabled) { return(false); } //if (AbpCrossCuttingConcerns.IsApplied(invocation.TargetObject, AbpCrossCuttingConcerns.Auditing)) //{ // return false; //} if (!auditingHelper.ShouldSaveAudit(invocation.Method)) { return(false); } return(true); }
public void Invoke() { var next = Substitute.For <RequestDelegate>(); var manager = Substitute.For <IAuditingManager>(); var handle = Substitute.For <IAuditSaveHandle>(); manager.BeginScope().Returns(handle); var options = new AuditingOptions(); var middleware = new AuditingMiddleware(next, manager, new OptionsWrapper <AuditingOptions>(options)); Should.NotThrow(() => middleware.Invoke(new DefaultHttpContext())); handle.ReceivedWithAnyArgs(1).SaveAsync(); options.IsEnabled = false; handle.ClearReceivedCalls(); Should.NotThrow(() => middleware.Invoke(new DefaultHttpContext())); handle.ReceivedWithAnyArgs(0).SaveAsync(); options.IsEnabled = true; options.IsEnabledForAnonymousUsers = false; handle.ClearReceivedCalls(); Should.NotThrow(() => middleware.Invoke(new DefaultHttpContext())); handle.ReceivedWithAnyArgs(0).SaveAsync(); }
//protected ICorrelationIdProvider CorrelationIdProvider { get; } public AuditingFactory( IOptions <AuditingOptions> options, ICurrentUser currentUser, ICurrentTenant currentTenant, //ICurrentClient currentClient, IClock clock, //IAuditingStore auditingStore, ILogger <AuditingFactory> logger, IServiceProvider serviceProvider //ICorrelationIdProvider correlationIdProvider ) { Options = options.Value; CurrentUser = currentUser; CurrentTenant = currentTenant; //CurrentClient = currentClient; Clock = clock; //AuditingStore = auditingStore; Logger = logger; ServiceProvider = serviceProvider; //CorrelationIdProvider = correlationIdProvider; }
private bool ShouldWriteAuditLog( IMethodInvocation invocation, AuditingOptions options, ICurrentUser currentUser, bool hasError) { if (options.AlwaysLogOnException && hasError) { return(true); } if (!options.IsEnabledForAnonymousUsers && !currentUser.IsAuthenticated) { return(false); } if (!options.IsEnabledForGetRequests && invocation.Method.Name.StartsWith("Get", StringComparison.OrdinalIgnoreCase)) { return(false); } return(true); }
public AuditTestController_Tests() { _options = ServiceProvider.GetRequiredService <IOptions <AuditingOptions> >().Value; _auditingStore = ServiceProvider.GetRequiredService <IAuditingStore>(); }