123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Data.Entity.Infrastructure;
- using EMIS.Utility.FormValidate;
- using Bowin.Common.Data;
- using System.Web;
- using System.Data;
- using System.Linq.Expressions;
- using EMIS.Entities.Log;
- namespace EMIS.Utility.Log
- {
- public class DbLog
- {
- public static List<Log_Operate> GetBatchUpdateLog<T>(IList<T> entities, System.Data.Entity.DbContext ctx) where T : class
- {
- var result = new List<Log_Operate>();
- var principal = CustomPrincipal.Current;
- var operateUserID = (principal != null) ? (Guid?)principal.UserID : null;
- var tableKey = EMIS.Entities.TableKeyDictionary.GetKeyName<T>();
- ParameterExpression keyParamter = Expression.Parameter(typeof(DbEntityEntry<T>), "w");
- MemberExpression keyEntryExpression = Expression.Property(keyParamter, "Entity");
- MemberExpression keyExpression = Expression.Property(keyEntryExpression, tableKey);
- var entryList = ctx.ChangeTracker.Entries<T>().ToList();
- var originalValues = entities.Select(x => new {
- OriginalValues = entryList.FirstOrDefault(
- Expression.Lambda<Func<DbEntityEntry<T>, bool>>(
- Expression.Equal(keyExpression, Expression.Constant((Guid)typeof(T).GetProperty(tableKey).GetValue(x, null)))
- , keyParamter).Compile()).OriginalValues,
- CurrentValues = x
- }).ToList();
- foreach (var updateChange in originalValues)
- {
- var log = new Log_Operate
- {
- OperateID = Guid.NewGuid(),
- UserID = operateUserID,
- IP = ApplicationClientHelper.GetIP(),
- TableName = updateChange.CurrentValues.GetType().Name,
- SourceUrl = (HttpContext.Current == null) ? null : HttpContext.Current.Request.UrlReferrer.OriginalString,
- OperateTime = DateTime.Now
- };
- log.Operate = "修改";
- log.Detail = "修改前内容(" + GatherValues(updateChange.OriginalValues) + "),";
- log.Detail += "修改后内容(" + GatherValues(updateChange.CurrentValues) + ")";
- result.Add(log);
- }
- return result;
- }
- public static List<Log_Operate> GetBulkInsertLog<T>(IList<T> entities)
- {
- var table = entities.ToTable();
- return GetBulkInsertLog(table, typeof(T).Name);
- }
- public static List<Log_Operate> GetBulkInsertLog(DataTable table, string tableName)
- {
- var result = new List<Log_Operate>();
- var principal = CustomPrincipal.Current;
- var operateUserID = (principal != null) ? (Guid?)principal.UserID : null;
- var myIP = ApplicationClientHelper.GetIP();
- foreach (DataRow row in table.Rows)
- {
- var value = "详细内容(";
- for (int i = 0; i < table.Columns.Count; i++)
- {
- string columnName = table.Columns[i].ColumnName;
- value += columnName + ":";
- value += row[columnName].ToString() + "|";
- }
- if (value.EndsWith("|"))
- {
- value = value.TrimEnd('|');
- }
- value += ")";
- Log_Operate operate = new Log_Operate()
- {
- OperateID = Guid.NewGuid(),
- UserID = operateUserID,
- IP = myIP,
- TableName = tableName,
- SourceUrl = (HttpContext.Current == null) ? null : HttpContext.Current.Request.UrlReferrer.OriginalString,
- OperateTime = DateTime.Now,
- Operate = "新增",
- Detail = value
- };
- result.Add(operate);
- }
- return result;
- }
- public static List<Log_Operate> GetDeleteLog<T>(IList<T> entities)
- {
- var table = entities.ToTable();
- return GetDeleteLog(table, typeof(T).Name);
- }
- public static List<Log_Operate> GetDeleteLog(DataTable table, string tableName)
- {
- var result = new List<Log_Operate>();
- var principal = CustomPrincipal.Current;
- var operateUserID = (principal != null) ? (Guid?)principal.UserID : null;
- var myIP = ApplicationClientHelper.GetIP();
- foreach (DataRow row in table.Rows)
- {
- var value = "删除内容(";
- for (int i = 0; i < table.Columns.Count; i++)
- {
- string columnName = table.Columns[i].ColumnName;
- value += columnName + ":";
- value += row[columnName].ToString() + "|";
- }
- if (value.EndsWith("|"))
- {
- value = value.TrimEnd('|');
- }
- value += ")";
- Log_Operate operate = new Log_Operate()
- {
- OperateID = Guid.NewGuid(),
- UserID = operateUserID,
- IP = myIP,
- TableName = tableName,
- SourceUrl = (HttpContext.Current == null) ? null : HttpContext.Current.Request.UrlReferrer.OriginalString,
- OperateTime = DateTime.Now,
- Operate = "删除",
- Detail = value
- };
- result.Add(operate);
- }
- return result;
- }
- public static List<Log_Operate> GetLog(DbChangeTracker changeTracker)
- {
- var result = new List<Log_Operate>();
- var entryList = changeTracker.Entries().ToList();
- var principal = CustomPrincipal.Current;
- entryList.ForEach(x => AddLog(x, result, principal));
- return result;
- }
- public static List<Log_Operate> GetRelationshipLog(System.Data.Entity.DbContext ctx)
- {
- var result = new List<Log_Operate>();
- var principal = CustomPrincipal.Current;
- var operateUserID = (principal != null) ? (Guid?)principal.UserID : null;
- var objectContext = ((IObjectContextAdapter)ctx).ObjectContext;
- var deleteChanges = objectContext.ObjectStateManager.GetObjectStateEntries(System.Data.Entity.EntityState.Deleted);
- var addChanges = objectContext.ObjectStateManager.GetObjectStateEntries(System.Data.Entity.EntityState.Added);
- var myIP = ApplicationClientHelper.GetIP();
- foreach(var deleteChange in deleteChanges)
- {
- if (deleteChange.IsRelationship)
- {
- var log = new Log_Operate
- {
- OperateID = Guid.NewGuid(),
- UserID = operateUserID,
- IP = myIP,
- TableName = deleteChange.EntitySet.Name,
- SourceUrl = (HttpContext.Current == null) ? null : HttpContext.Current.Request.UrlReferrer.OriginalString,
- OperateTime = DateTime.Now,
- Operate = "删除",
- Detail = "删除内容(" + GatherValues(deleteChange.OriginalValues) + ")"
- };
- result.Add(log);
- }
- }
- foreach (var addChange in addChanges)
- {
- if (addChange.IsRelationship)
- {
- var log = new Log_Operate
- {
- OperateID = Guid.NewGuid(),
- UserID = operateUserID,
- IP = myIP,
- TableName = addChange.EntitySet.Name,
- SourceUrl = (HttpContext.Current == null) ? null : HttpContext.Current.Request.UrlReferrer.OriginalString,
- OperateTime = DateTime.Now,
- Operate = "新增",
- Detail = "详细内容(" + GatherRelationValues(addChange.CurrentValues, objectContext) + ")"
- };
- result.Add(log);
- }
- }
- return result;
- }
- private static void AddLog(DbEntityEntry entry, IList<Log_Operate> logList, CustomPrincipal principal)
- {
- var operateUserID = (principal != null) ? (Guid?)principal.UserID : null;
- var log = new Log_Operate {
- OperateID = Guid.NewGuid(),
- UserID = operateUserID,
- IP = ApplicationClientHelper.GetIP(),
- TableName = entry.Entity.GetType().Name,
- SourceUrl = (HttpContext.Current == null) ? null : HttpContext.Current.Request.UrlReferrer.OriginalString,
- OperateTime = DateTime.Now
- };
- switch (entry.State)
- {
- case System.Data.Entity.EntityState.Added:
- log.Operate = "新增";
- log.Detail = "详细内容(" + GatherValues(entry.CurrentValues) + ")";
- break;
- case System.Data.Entity.EntityState.Modified:
- log.Operate = "修改";
- log.Detail = "修改前内容(" + GatherValues(entry.OriginalValues) + "),";
- log.Detail += "修改后内容(" + GatherValues(entry.CurrentValues) + ")";
- break;
- case System.Data.Entity.EntityState.Deleted:
- log.Operate = "删除";
- log.Detail = "删除内容(" + GatherValues(entry.OriginalValues) + ")";
- break;
- default:
- return;
- }
- logList.Add(log);
- }
- private static string GatherRelationValues(System.Data.Common.DbDataRecord values,
- System.Data.Entity.Core.Objects.ObjectContext context)
- {
- var result = "";
- for (int i = 0; i < values.FieldCount; i ++ )
- {
- var entry = (System.Data.Entity.Core.EntityKey)values.GetValue(i);
- result += entry.EntitySetName + ":";
- var entity = context.GetObjectByKey(entry);
- var entityType = entity.GetType();
- var propertyName = entry.GetEntitySet(context.MetadataWorkspace).ElementType.KeyMembers[0].Name;
- var property = entityType.GetProperty(propertyName);
- result += property.GetValue(entity, null) + "|";
- //foreach (var property in entityType.GetProperties())
- //{
- // result += property.Name + ":" + property.GetValue(entity, null) + "|";
- //}
- }
- if (result.EndsWith("|"))
- {
- result = result.TrimEnd('|');
- }
- return result;
- }
- private static string GatherValues(System.Data.Common.DbDataRecord values)
- {
- var result = "";
- for (int i = 0; i < values.FieldCount; i ++ )
- {
- var entry = (System.Data.Entity.Core.EntityKey)values.GetValue(i);
- result += entry.EntitySetName + ":" + entry.EntityKeyValues[0].Value.ToString() + "|";
- }
- if (result.EndsWith("|"))
- {
- result = result.TrimEnd('|');
- }
- return result;
- }
- private static string GatherValues(DbPropertyValues values)
- {
- var result = "";
- foreach (var propertyName in values.PropertyNames)
- {
- result += propertyName + ":" + values[propertyName] + "|";
- }
- if (result.EndsWith("|"))
- {
- result = result.TrimEnd('|');
- }
- return result;
- }
- private static string GatherValues<T>(T entity)
- {
- var result = "";
- var properties = typeof(T).GetProperties();
- foreach (var property in properties)
- {
- if (property.PropertyType.IsPrimitive || property.PropertyType.IsValueType || property.PropertyType.Name.StartsWith("String"))
- {
- result += property.Name + ":" + (property.GetValue(entity, null) ?? "").ToString() + "|";
- }
- }
- if (result.EndsWith("|"))
- {
- result = result.TrimEnd('|');
- }
- return result;
- }
- }
- }
|