DbLog.cs 13 KB

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