Exemple #1
0
 /// <summary>
 /// Saves the scope asynchronously.
 /// </summary>
 public async Task SaveScopeAsync(IAuditDbContext context, IAuditScope scope, EntityFrameworkEvent @event)
 {
     UpdateAuditEvent(@event, context);
     (scope.Event as AuditEventEntityFramework).EntityFrameworkEvent = @event;
     context.OnScopeSaving(scope);
     await scope.SaveAsync();
 }
 public override InterceptionResult <int> SavingChanges(DbContextEventData eventData, InterceptionResult <int> result)
 {
     _auditContext = new DefaultAuditContext(eventData.Context);
     _helper.SetConfig(_auditContext);
     _auditScope = _helper.BeginSaveChanges(_auditContext);
     return(base.SavingChanges(eventData, result));
 }
Exemple #3
0
 /// <summary>
 /// Saves the scope.
 /// </summary>
 public void SaveScope(IAuditDbContext context, IAuditScope scope, EntityFrameworkEvent @event)
 {
     UpdateAuditEvent(@event, context);
     (scope.Event as AuditEventEntityFramework).EntityFrameworkEvent = @event;
     context.OnScopeSaving(scope);
     scope.Save();
 }
Exemple #4
0
 public override void OnScopeSaving(IAuditScope auditScope)
 {
     _logger.LogInformation("Audit event recorded: {event}", new
     {
         IPAddress = _accessor.HttpContext?.Connection?.RemoteIpAddress?.ToString(), auditScope.Event
     });
 }
        public override async ValueTask <InterceptionResult <int> > SavingChangesAsync(DbContextEventData eventData, InterceptionResult <int> result, CancellationToken cancellationToken = default)
        {
            _auditContext = new DefaultAuditContext(eventData.Context);
            _helper.SetConfig(_auditContext);
            _auditScope = await _helper.BeginSaveChangesAsync(_auditContext);

            return(await base.SavingChangesAsync(eventData, result, cancellationToken));
        }
 public override void OnScopeSaving(IAuditScope auditScope)
 {
     if (auditScope.Event.GetEntityFrameworkEvent().Entries[0].ColumnValues.ContainsKey("BloggerName") &&
         auditScope.Event.GetEntityFrameworkEvent().Entries[0].ColumnValues["BloggerName"].Equals("ROLLBACK"))
     {
         Database.CurrentTransaction.Rollback();
     }
     else
     {
         Database.CurrentTransaction.Commit();
     }
 }
Exemple #7
0
        public async Task EndSaveChangesAsync(IAuditDbContext context, IAuditScope scope, int result, Exception exception = null)
        {
            var efEvent = scope.GetEntityFrameworkEvent();

            if (efEvent == null)
            {
                return;
            }
            efEvent.Success      = exception == null;
            efEvent.Result       = result;
            efEvent.ErrorMessage = exception?.GetExceptionInfo();
            await SaveScopeAsync(context, scope, efEvent);
        }
Exemple #8
0
        public override InterceptionResult <DbDataReader> ReaderExecuting(DbCommand command, CommandEventData eventData, InterceptionResult <DbDataReader> result)
        {
            if (Core.Configuration.AuditDisabled || ExcludeReaderEvents)
            {
                return(result);
            }
            var auditEvent = new AuditEventCommandEntityFramework
            {
                CommandEvent = CreateEvent(command, eventData)
            };

            _currentScope = CreateAuditScope(auditEvent);
            return(result);
        }
Exemple #9
0
        public async override ValueTask <InterceptionResult <DbDataReader> > ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult <DbDataReader> result, CancellationToken cancellationToken = default)
#endif
        {
            if (Core.Configuration.AuditDisabled || ExcludeReaderEvents)
            {
                return(await base.ReaderExecutingAsync(command, eventData, result, cancellationToken));
            }
            var auditEvent = new AuditEventCommandEntityFramework
            {
                CommandEvent = CreateEvent(command, eventData)
            };

            _currentScope = await CreateAuditScopeAsync(auditEvent);

            return(await base.ReaderExecutingAsync(command, eventData, result, cancellationToken));
        }
 /// <summary>
 /// Gets the Low-Level EF Command Event portion of the Audit Event on the given scope.
 /// </summary>
 /// <param name="auditScope">The audit scope.</param>
 public static CommandEvent GetCommandEntityFrameworkEvent(this IAuditScope auditScope)
 {
     return(auditScope?.Event.GetCommandEntityFrameworkEvent());
 }
 /// <summary>
 /// Called after the EF operation execution and before the AuditScope saving.
 /// Override to specify custom logic.
 /// </summary>
 /// <param name="auditScope">The audit scope.</param>
 public virtual void OnScopeSaving(IAuditScope auditScope)
 {
 }
 /// <summary>
 /// Called after the audit scope is created.
 /// Override to specify custom logic.
 /// </summary>
 /// <param name="auditScope">The audit scope.</param>
 public virtual void OnScopeCreated(IAuditScope auditScope)
 {
 }
 public override void OnScopeCreated(IAuditScope auditScope)
 {
     Database.BeginTransaction();
 }
Exemple #14
0
 /// <summary>
 /// Ends the event for asynchronous interceptions.
 /// </summary>
 private static void EndAsyncAuditInterceptEvent(Task task, IInvocation invocation, InterceptEvent intEvent, IAuditScope scope, object result)
 {
     intEvent.AsyncStatus = task.Status.ToString();
     if (task.Status == TaskStatus.Faulted)
     {
         intEvent.Exception = task.Exception?.GetExceptionInfo();
     }
     else if (task.Status == TaskStatus.RanToCompletion)
     {
         SuccessAuditInterceptEvent(invocation, intEvent, result);
     }
     scope.Dispose();
 }
Exemple #15
0
        /// <summary>
        /// Intercept an asynchronous operation that returns a Task Of[T].
        /// </summary>
        private static async Task <T> InterceptAsync <T>(Task <T> task, IInvocation invocation, InterceptEvent intEvent, IAuditScope scope)
        {
            T result;

            try
            {
                result = await task.ConfigureAwait(false);
            }
            catch
            {
                EndAsyncAuditInterceptEvent(task, invocation, intEvent, scope, null);
                throw;
            }
            EndAsyncAuditInterceptEvent(task, invocation, intEvent, scope, result);
            return(result);
        }
Exemple #16
0
 /// <summary>
 /// Intercept an asynchronous operation that returns a Task.
 /// </summary>
 private static async Task InterceptAsync(Task task, IInvocation invocation, InterceptEvent intEvent, IAuditScope scope)
 {
     try
     {
         await task.ConfigureAwait(false);
     }
     catch
     {
         EndAsyncAuditInterceptEvent(task, invocation, intEvent, scope, null);
         throw;
     }
     EndAsyncAuditInterceptEvent(task, invocation, intEvent, scope, "Void");
 }