DbLog.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data.Entity.Infrastructure;
  5. using EMIS.Utility.FormValidate;
  6. using Bowin.Common.Data;
  7. using System.Web;
  8. using System.Data;
  9. using System.Linq.Expressions;
  10. using EMIS.Entities.Log;
  11. namespace EMIS.Utility.Log
  12. {
  13. public class DbLog
  14. {
  15. public static List<Log_Operate> GetBatchUpdateLog<T>(IList<T> entities, System.Data.Entity.DbContext ctx) where T : class
  16. {
  17. var result = new List<Log_Operate>();
  18. var principal = CustomPrincipal.Current;
  19. var operateUserID = (principal != null) ? (Guid?)principal.UserID : null;
  20. var tableKey = EMIS.Entities.TableKeyDictionary.GetKeyName<T>();
  21. ParameterExpression keyParamter = Expression.Parameter(typeof(DbEntityEntry<T>), "w");
  22. MemberExpression keyEntryExpression = Expression.Property(keyParamter, "Entity");
  23. MemberExpression keyExpression = Expression.Property(keyEntryExpression, tableKey);
  24. var entryList = ctx.ChangeTracker.Entries<T>().ToList();
  25. var originalValues = entities.Select(x => new {
  26. OriginalValues = entryList.FirstOrDefault(
  27. Expression.Lambda<Func<DbEntityEntry<T>, bool>>(
  28. Expression.Equal(keyExpression, Expression.Constant((Guid)typeof(T).GetProperty(tableKey).GetValue(x, null)))
  29. , keyParamter).Compile()).OriginalValues,
  30. CurrentValues = x
  31. }).ToList();
  32. foreach (var updateChange in originalValues)
  33. {
  34. var log = new Log_Operate
  35. {
  36. OperateID = Guid.NewGuid(),
  37. UserID = operateUserID,
  38. IP = ApplicationClientHelper.GetIP(),
  39. TableName = updateChange.CurrentValues.GetType().Name,
  40. SourceUrl = (HttpContext.Current == null) ? null : HttpContext.Current.Request.UrlReferrer.OriginalString,
  41. OperateTime = DateTime.Now
  42. };
  43. log.Operate = "修改";
  44. log.Detail = "修改前内容(" + GatherValues(updateChange.OriginalValues) + "),";
  45. log.Detail += "修改后内容(" + GatherValues(updateChange.CurrentValues) + ")";
  46. result.Add(log);
  47. }
  48. return result;
  49. }
  50. public static List<Log_Operate> GetBulkInsertLog<T>(IList<T> entities)
  51. {
  52. var table = entities.ToTable();
  53. return GetBulkInsertLog(table, typeof(T).Name);
  54. }
  55. public static List<Log_Operate> GetBulkInsertLog(DataTable table, string tableName)
  56. {
  57. var result = new List<Log_Operate>();
  58. var principal = CustomPrincipal.Current;
  59. var operateUserID = (principal != null) ? (Guid?)principal.UserID : null;
  60. var myIP = ApplicationClientHelper.GetIP();
  61. foreach (DataRow row in table.Rows)
  62. {
  63. var value = "详细内容(";
  64. for (int i = 0; i < table.Columns.Count; i++)
  65. {
  66. string columnName = table.Columns[i].ColumnName;
  67. value += columnName + ":";
  68. value += row[columnName].ToString() + "|";
  69. }
  70. if (value.EndsWith("|"))
  71. {
  72. value = value.TrimEnd('|');
  73. }
  74. value += ")";
  75. Log_Operate operate = new Log_Operate()
  76. {
  77. OperateID = Guid.NewGuid(),
  78. UserID = operateUserID,
  79. IP = myIP,
  80. TableName = tableName,
  81. SourceUrl = (HttpContext.Current == null) ? null : HttpContext.Current.Request.UrlReferrer.OriginalString,
  82. OperateTime = DateTime.Now,
  83. Operate = "新增",
  84. Detail = value
  85. };
  86. result.Add(operate);
  87. }
  88. return result;
  89. }
  90. public static List<Log_Operate> GetDeleteLog<T>(IList<T> entities)
  91. {
  92. var table = entities.ToTable();
  93. return GetDeleteLog(table, typeof(T).Name);
  94. }
  95. public static List<Log_Operate> GetDeleteLog(DataTable table, string tableName)
  96. {
  97. var result = new List<Log_Operate>();
  98. var principal = CustomPrincipal.Current;
  99. var operateUserID = (principal != null) ? (Guid?)principal.UserID : null;
  100. var myIP = ApplicationClientHelper.GetIP();
  101. foreach (DataRow row in table.Rows)
  102. {
  103. var value = "删除内容(";
  104. for (int i = 0; i < table.Columns.Count; i++)
  105. {
  106. string columnName = table.Columns[i].ColumnName;
  107. value += columnName + ":";
  108. value += row[columnName].ToString() + "|";
  109. }
  110. if (value.EndsWith("|"))
  111. {
  112. value = value.TrimEnd('|');
  113. }
  114. value += ")";
  115. Log_Operate operate = new Log_Operate()
  116. {
  117. OperateID = Guid.NewGuid(),
  118. UserID = operateUserID,
  119. IP = myIP,
  120. TableName = tableName,
  121. SourceUrl = (HttpContext.Current == null) ? null : HttpContext.Current.Request.UrlReferrer.OriginalString,
  122. OperateTime = DateTime.Now,
  123. Operate = "删除",
  124. Detail = value
  125. };
  126. result.Add(operate);
  127. }
  128. return result;
  129. }
  130. public static List<Log_Operate> GetLog(DbChangeTracker changeTracker)
  131. {
  132. var result = new List<Log_Operate>();
  133. var entryList = changeTracker.Entries().ToList();
  134. var principal = CustomPrincipal.Current;
  135. entryList.ForEach(x => AddLog(x, result, principal));
  136. return result;
  137. }
  138. public static List<Log_Operate> GetRelationshipLog(System.Data.Entity.DbContext ctx)
  139. {
  140. var result = new List<Log_Operate>();
  141. var principal = CustomPrincipal.Current;
  142. var operateUserID = (principal != null) ? (Guid?)principal.UserID : null;
  143. var objectContext = ((IObjectContextAdapter)ctx).ObjectContext;
  144. var deleteChanges = objectContext.ObjectStateManager.GetObjectStateEntries(System.Data.Entity.EntityState.Deleted);
  145. var addChanges = objectContext.ObjectStateManager.GetObjectStateEntries(System.Data.Entity.EntityState.Added);
  146. var myIP = ApplicationClientHelper.GetIP();
  147. foreach(var deleteChange in deleteChanges)
  148. {
  149. if (deleteChange.IsRelationship)
  150. {
  151. var log = new Log_Operate
  152. {
  153. OperateID = Guid.NewGuid(),
  154. UserID = operateUserID,
  155. IP = myIP,
  156. TableName = deleteChange.EntitySet.Name,
  157. SourceUrl = (HttpContext.Current == null) ? null : HttpContext.Current.Request.UrlReferrer.OriginalString,
  158. OperateTime = DateTime.Now,
  159. Operate = "删除",
  160. Detail = "删除内容(" + GatherValues(deleteChange.OriginalValues) + ")"
  161. };
  162. result.Add(log);
  163. }
  164. }
  165. foreach (var addChange in addChanges)
  166. {
  167. if (addChange.IsRelationship)
  168. {
  169. var log = new Log_Operate
  170. {
  171. OperateID = Guid.NewGuid(),
  172. UserID = operateUserID,
  173. IP = myIP,
  174. TableName = addChange.EntitySet.Name,
  175. SourceUrl = (HttpContext.Current == null) ? null : HttpContext.Current.Request.UrlReferrer.OriginalString,
  176. OperateTime = DateTime.Now,
  177. Operate = "新增",
  178. Detail = "详细内容(" + GatherRelationValues(addChange.CurrentValues, objectContext) + ")"
  179. };
  180. result.Add(log);
  181. }
  182. }
  183. return result;
  184. }
  185. private static void AddLog(DbEntityEntry entry, IList<Log_Operate> logList, CustomPrincipal principal)
  186. {
  187. var operateUserID = (principal != null) ? (Guid?)principal.UserID : null;
  188. var log = new Log_Operate {
  189. OperateID = Guid.NewGuid(),
  190. UserID = operateUserID,
  191. IP = ApplicationClientHelper.GetIP(),
  192. TableName = entry.Entity.GetType().Name,
  193. SourceUrl = (HttpContext.Current == null) ? null : HttpContext.Current.Request.UrlReferrer.OriginalString,
  194. OperateTime = DateTime.Now
  195. };
  196. switch (entry.State)
  197. {
  198. case System.Data.Entity.EntityState.Added:
  199. log.Operate = "新增";
  200. log.Detail = "详细内容(" + GatherValues(entry.CurrentValues) + ")";
  201. break;
  202. case System.Data.Entity.EntityState.Modified:
  203. log.Operate = "修改";
  204. log.Detail = "修改前内容(" + GatherValues(entry.OriginalValues) + "),";
  205. log.Detail += "修改后内容(" + GatherValues(entry.CurrentValues) + ")";
  206. break;
  207. case System.Data.Entity.EntityState.Deleted:
  208. log.Operate = "删除";
  209. log.Detail = "删除内容(" + GatherValues(entry.OriginalValues) + ")";
  210. break;
  211. default:
  212. return;
  213. }
  214. logList.Add(log);
  215. }
  216. private static string GatherRelationValues(System.Data.Common.DbDataRecord values,
  217. System.Data.Entity.Core.Objects.ObjectContext context)
  218. {
  219. var result = "";
  220. for (int i = 0; i < values.FieldCount; i ++ )
  221. {
  222. var entry = (System.Data.Entity.Core.EntityKey)values.GetValue(i);
  223. result += entry.EntitySetName + ":";
  224. var entity = context.GetObjectByKey(entry);
  225. var entityType = entity.GetType();
  226. var propertyName = entry.GetEntitySet(context.MetadataWorkspace).ElementType.KeyMembers[0].Name;
  227. var property = entityType.GetProperty(propertyName);
  228. result += property.GetValue(entity, null) + "|";
  229. //foreach (var property in entityType.GetProperties())
  230. //{
  231. // result += property.Name + ":" + property.GetValue(entity, null) + "|";
  232. //}
  233. }
  234. if (result.EndsWith("|"))
  235. {
  236. result = result.TrimEnd('|');
  237. }
  238. return result;
  239. }
  240. private static string GatherValues(System.Data.Common.DbDataRecord values)
  241. {
  242. var result = "";
  243. for (int i = 0; i < values.FieldCount; i ++ )
  244. {
  245. var entry = (System.Data.Entity.Core.EntityKey)values.GetValue(i);
  246. result += entry.EntitySetName + ":" + entry.EntityKeyValues[0].Value.ToString() + "|";
  247. }
  248. if (result.EndsWith("|"))
  249. {
  250. result = result.TrimEnd('|');
  251. }
  252. return result;
  253. }
  254. private static string GatherValues(DbPropertyValues values)
  255. {
  256. var result = "";
  257. foreach (var propertyName in values.PropertyNames)
  258. {
  259. result += propertyName + ":" + values[propertyName] + "|";
  260. }
  261. if (result.EndsWith("|"))
  262. {
  263. result = result.TrimEnd('|');
  264. }
  265. return result;
  266. }
  267. private static string GatherValues<T>(T entity)
  268. {
  269. var result = "";
  270. var properties = typeof(T).GetProperties();
  271. foreach (var property in properties)
  272. {
  273. if (property.PropertyType.IsPrimitive || property.PropertyType.IsValueType || property.PropertyType.Name.StartsWith("String"))
  274. {
  275. result += property.Name + ":" + (property.GetValue(entity, null) ?? "").ToString() + "|";
  276. }
  277. }
  278. if (result.EndsWith("|"))
  279. {
  280. result = result.TrimEnd('|');
  281. }
  282. return result;
  283. }
  284. }
  285. }