示例#1
0
        /// <summary>
        /// Inserts AuditLog into the database
        /// </summary>
        /// <param name="auditTrailLog"></param>
        /// <returns> <c>True</c> if log is saved succesfully into the database; otherwise, <c>false</c></returns>
        public async Task <bool> AddLogAsync(IAuditTrailLog auditTrailLog)
        {
            if (auditTrailLog != null)
            {
                await _dsStorageAdapter.AddAsync((DocumentStoreAuditTrailLog)auditTrailLog);

                return(true);
            }
            return(false);
        }
        /// <summary>
        /// Add audit logs with delta changes
        /// </summary>
        /// <param name="action">Audit action <see cref="AuditAction"/>.</param>
        /// <param name="id">Key Id for entity</param>
        /// <param name="before">Entity before changes occured. You must pass empty instance when action is Add.</param>
        /// <param name="after">Entity after changes occured. You must pass empty instance when action is Delete.</param>
        public async Task AddAuditLogAsync(AuditAction action, string id, T before, T after)
        {
            Validate(action, id, before, after);

            CompareLogic compareLogic = new CompareLogic();

            compareLogic.Config.MaxDifferences = 100;
            compareLogic.Config.CaseSensitive  = false;
            compareLogic.Config.TreatStringEmptyAndNullTheSame = true;

            ComparisonResult comapreResults = compareLogic.Compare(before, after);

            List <AuditDelta> deltaList = new List <AuditDelta>();

            foreach (var change in comapreResults.Differences)
            {
                AuditDelta delta = new AuditDelta
                {
                    FieldName = change.PropertyName.Replace(change.ParentPropertyName + ".", string.Empty),
                    Value     = change.Object2Value
                };
                deltaList.Add(delta);
            }

            AuditTable auditTable = new AuditTable()
            {
                _id             = Guid.NewGuid(),
                EntityId        = id,
                EntityName      = typeof(T).Name.ToLower(),
                AuditActionType = (int)action,
                DateTimeStamp   = DateTime.UtcNow,
                Changes         = JsonConvert.SerializeObject(deltaList)
            };

            await dsStorageAdapter.AddAsync(auditTable);
        }