Ejemplo n.º 1
0
        public bool AuditTrail(ZOperationResult operationResult, string logDomain, string logEntity, string logOperation, IZDataBase entityBefore, IZDataBase entityAfter)
        {
            if (IsAuditTrail(logDomain, logEntity, logOperation))
            {
                JsonSerializerSettings settings = new JsonSerializerSettings
                {
                    //Formatting = Formatting.Indented
                    Formatting = Formatting.None,
                    MaxDepth   = 1
                };

                object[] ids;
                ZDataDictionaryAttribute dictionary;
                if (entityAfter != null)
                {
                    ids        = entityAfter.GetId();
                    dictionary = DataHelper.GetDataDictionaryAttribute(entityAfter.GetType());
                }
                else // entityBefore != null
                {
                    ids        = entityBefore.GetId();
                    dictionary = DataHelper.GetDataDictionaryAttribute(entityBefore.GetType());
                }
                // {"Id1":1,"Id2":2}
                string logId   = "";
                int    idIndex = 0;
                foreach (string idProperty in dictionary.Keys)
                {
                    logId += (String.IsNullOrEmpty(logId) ? "" : ",") + "\"" + idProperty + "\":" + JsonConvert.SerializeObject(ids[idIndex++], settings);
                }
                logId = "{" + logId + "}";

                AuditTrailLog auditTrailLog = new AuditTrailLog();
                auditTrailLog.LogDate         = DateTime.Today;
                auditTrailLog.LogTime         = DateTime.Now;
                auditTrailLog.LogUserName     = IdentityHelper.UserName;
                auditTrailLog.LogDomain       = logDomain;
                auditTrailLog.LogEntity       = logEntity;
                auditTrailLog.LogOperation    = logOperation;
                auditTrailLog.LogId           = logId;
                auditTrailLog.LogEntityBefore = entityBefore == null ? "" : JsonConvert.SerializeObject(entityBefore, settings);
                auditTrailLog.LogEntityAfter  = entityAfter == null ? "" : JsonConvert.SerializeObject(entityAfter, settings);

                IGenericRepository <AuditTrailLog> repository = UnitOfWork.GetRepository <AuditTrailLog>();
                if (repository.Create(operationResult, auditTrailLog))
                {
                    UnitOfWork.Save(operationResult);
                }
            }

            return(operationResult.Ok);
        }
Ejemplo n.º 2
0
        public bool AuditTrail(ZOperationResult operationResult, string logUserName, string logDomain, string logEntity, string logOperation, IZDataBase entityBefore, IZDataBase entityAfter)
        {
            string logMode;

            if (IsAuditTrail(logDomain, logEntity, logOperation, out logMode))
            {
                // (N) None
                // (K) Entity Key
                // (E) Full Entity
                if (!(String.IsNullOrEmpty(logMode) || logMode == "N"))
                {
                    JsonSerializerSettings jsonSettings = new JsonSerializerSettings
                    {
                        //Formatting = Formatting.Indented
                        Formatting = Formatting.None,
                        MaxDepth   = 1
                    };

                    object[]  ids;
                    IZProfile profile;
                    if (entityAfter != null)
                    {
                        ids     = entityAfter.GetId();
                        profile = DataHelper.GetProfile(entityAfter.GetType());
                    }
                    else // entityBefore != null
                    {
                        ids     = entityBefore.GetId();
                        profile = DataHelper.GetProfile(entityBefore.GetType());
                    }

                    // 1|2
                    string logId   = "";
                    int    idIndex = 0;
                    foreach (string idProperty in profile.Keys)
                    {
                        logId += (String.IsNullOrEmpty(logId) ? "" : "|") + JsonConvert.SerializeObject(ids[idIndex++], jsonSettings);
                    }

                    // {"Id1":1,"Id2":2}
                    //string logId = "";
                    //int idIndex = 0;
                    //foreach (string idProperty in profile.Keys)
                    //{
                    //    logId += (String.IsNullOrEmpty(logId) ? "" : ",") + "\"" + idProperty + "\":" + JsonConvert.SerializeObject(ids[idIndex++], settings);
                    //}
                    //logId = "{" + logId + "}";

                    AuditTrailLog auditTrailLog = new AuditTrailLog();
                    auditTrailLog.LogDate      = DateTime.Today;
                    auditTrailLog.LogTime      = DateTime.Now;
                    auditTrailLog.LogUserName  = logUserName;
                    auditTrailLog.LogDomain    = logDomain;
                    auditTrailLog.LogEntity    = logEntity;
                    auditTrailLog.LogOperation = logOperation;
                    // K
                    auditTrailLog.LogId = logId;
                    // E
                    if (logMode == "E")
                    {
                        auditTrailLog.LogEntityBefore = entityBefore == null ? "" : JsonConvert.SerializeObject(entityBefore, jsonSettings);
                        auditTrailLog.LogEntityAfter  = entityAfter == null ? "" : JsonConvert.SerializeObject(entityAfter, jsonSettings);
                    }

                    IGenericRepository <AuditTrailLog> repository = UnitOfWork.GetRepository <AuditTrailLog>();
                    if (repository.Create(operationResult, auditTrailLog))
                    {
                        UnitOfWork.Save(operationResult);
                    }
                }
            }

            return(operationResult.Ok);
        }