private void InsertNewLog(IUnitOfWork uow, Row row, Row oldRow, AuditActionType auditActionType) { try { using (var auditLogConnection = SqlConnections.NewFor <AuditLogRow>()) { var fld = AuditLogRow.Fields; var entityId = (row as IIdRow).IdField[row] ?? 0; var lastVersion = auditLogConnection.TryFirst <AuditLogRow>(q => q .Select(fld.VersionNo, fld.NewEntity) .Where(fld.EntityTableName == row.Table && fld.EntityId == entityId) .OrderBy(fld.Id, desc: true)); //we don't want to serialize id field var pkField = (oldRow as IIdRow)?.IdField as Field; if (!(pkField is null)) { oldRow.ClearAssignment(pkField); } var oldrowJson = JsonConvert.SerializeObject(oldRow); var rowJson = JsonConvert.SerializeObject(row); if (auditActionType == AuditActionType.Delete || lastVersion?.NewEntity != rowJson) { int versionNo = (lastVersion?.VersionNo ?? 0) + 1; var auditLogRow = new AuditLogRow { VersionNo = versionNo, UserId = int.Parse(Authorization.UserId), ActionType = auditActionType, ActionDate = DateTime.Now, EntityTableName = row.Table, EntityId = entityId, OldEntity = oldrowJson, NewEntity = rowJson, IpAddress = HttpContext.Current.Request.UserHostAddress, SessionId = HttpContext.Current.Session.SessionID }; auditLogConnection.Insert <AuditLogRow>(auditLogRow); } } } catch (Exception ex) { Log.Debug("_Ext.AuditLog Failed.", ex, row.GetType()); } }
private void InsertNewLog(IUnitOfWork uow, Row row, Row oldRow, AuditActionType auditActionType) { try { using (var auditLogConnection = SqlConnections.NewFor <AuditLogRow>()) { var fld = AuditLogRow.Fields; var entityId = (row as IIdRow).IdField[row] ?? 0; var lastVersion = auditLogConnection.TryFirst <AuditLogRow>(q => q .Select(fld.VersionNo, fld.NewEntity) .Where(fld.EntityTableName == row.Table && fld.EntityId == entityId) .OrderBy(fld.Id, desc: true)); //var jsonSerializerSettings = new JsonSerializerSettings //{ // ContractResolver = new DynamicContractResolver("IDate", "IUser", "EDate", "EUser") //}; var rowJson = JsonConvert.SerializeObject(row); //, jsonSerializerSettings); var oldrowJson = JsonConvert.SerializeObject(oldRow); //, jsonSerializerSettings); if (auditActionType == AuditActionType.Delete || lastVersion?.NewEntity != rowJson) { int versionNo = (lastVersion?.VersionNo ?? 0) + 1; var auditLogRow = new AuditLogRow { VersionNo = versionNo, UserId = int.Parse(Authorization.UserId), ActionType = auditActionType, ActionDate = DateTime.Now, EntityTableName = row.Table, EntityId = entityId, OldEntity = oldrowJson, NewEntity = rowJson, IpAddress = HttpContext.Current.Request.UserHostAddress, SessionId = HttpContext.Current.Session.SessionID }; auditLogConnection.Insert <AuditLogRow>(auditLogRow); } } } catch (Exception ex) { Log.Debug("_Ext.AuditLog Failed.", ex, row.GetType()); } }