public async Task <bool> TryAudit(
            string correlationId,
            string userName,
            string referenceId,
            AuditDataType type,
            string newStateJson = null,
            string oldStateJson = null)
        {
            if (string.IsNullOrEmpty(newStateJson) && string.IsNullOrEmpty(oldStateJson))
            {
                _log?.WriteWarningAsync(nameof(AuditService), nameof(TryAudit),
                                        $"Unable to generate audit event based on both {nameof(newStateJson)} and {nameof(oldStateJson)} state as null.");
                return(false);
            }

            var auditModel = BuildAuditModel(correlationId, userName, DateTime.UtcNow, referenceId, type, newStateJson, oldStateJson);

            if (auditModel == null)
            {
                return(false);
            }

            await _auditRepository.InsertAsync(auditModel);

            return(true);
        }
        public DbAudit(AuditDataType auditType, BaseContext context)
        {
            String   iid;
            DateTime eventDate;

            context.GetObjectCreationValues(out iid, out eventDate);
            this.Iid            = iid;
            this.EventDate      = eventDate;
            this.SPID           = context.SPID;
            this.SpidLoginTime  = context.LoginTime;
            this.EventType      = auditType;
            this.WebLogin       = context.IsWebContext;
            this.ClrHostName    = context.CLRHostNameForSQL;
            this.ClrIP          = context.CLRIPAddressForSQL;
            this.ClientHostName = context.ClientHostNameForSQL;
            this.ClientIP       = context.ClientIPForSQL;
        }
        private static IAuditModel BuildAuditModel(
            string correlationId,
            string userName,
            DateTime timestamp,
            string referenceId,
            AuditDataType dataType,
            string newStateJson,
            string oldStateJson)
        {
            var eventType = AuditEventType.Edition;

            if (string.IsNullOrEmpty(oldStateJson))
            {
                eventType    = AuditEventType.Creation;
                oldStateJson = "{}";
            }

            if (string.IsNullOrEmpty(newStateJson))
            {
                eventType    = AuditEventType.Deletion;
                newStateJson = "{}";
            }

            var jdp        = new JsonDiffPatch();
            var diffResult = jdp.Diff(oldStateJson, newStateJson);

            if (string.IsNullOrEmpty(diffResult))
            {
                return(null);
            }

            return(new AuditModel
            {
                Timestamp = timestamp,
                CorrelationId = correlationId,
                UserName = userName,
                Type = eventType,
                DataType = dataType,
                DataReference = referenceId,
                DataDiff = diffResult
            });
        }
Exemple #4
0
        public async Task <bool> TryAudit(
            string correlationId,
            string userName,
            string referenceId,
            AuditDataType type,
            string newStateJson = null,
            string oldStateJson = null)
        {
            if (string.IsNullOrEmpty(newStateJson) && string.IsNullOrEmpty(oldStateJson))
            {
                return(false);
            }

            var auditModel = BuildAuditModel(correlationId, userName, DateTime.UtcNow, referenceId, type, newStateJson, oldStateJson);

            if (auditModel == null)
            {
                return(false);
            }

            await _auditRepository.InsertAsync(auditModel);

            return(true);
        }