public static string CompareModelToLog <T>(LogModule action, T newObj, T oldObj = null) where T : class { var result = string.Empty; if (oldObj == null) { result = string.Format("成功添加{0}!", action.ToString()); var props = typeof(T).GetProperties(); var idprop = props.FirstOrDefault(o => o.Name.ToUpper() == "ID"); var id = idprop.GetValue(newObj, null); result += string.Format("{0}Id:{1}{2}", Environment.NewLine, id, Environment.NewLine); foreach (var item in props) { var attr = item.GetCustomAttributes(typeof(OperationLogAttribute), true).FirstOrDefault(); if (attr != null) { var attrObj = (attr as OperationLogAttribute); object value = item.GetValue(newObj, null); if (attrObj.IsDict) { switch (attrObj.Source) { case DictSource.InDBSysDict: SysDataDictionaryDAL _dal = new SysDataDictionaryDAL(); _dal.GetExtModelByDicSN((int)value); break; case DictSource.Prop: var titleProp = props.FirstOrDefault(o => o.Name == attrObj.Prop); if (titleProp != null) { value = titleProp.GetValue(newObj, null); } break; case DictSource.Enum: var enumtitle = attrObj.ShowDicts.FirstOrDefault(o => o.Contains(value.ToString().ToLower() + ":")); if (enumtitle != null) { value = enumtitle.Split(':')[1]; } break; } } result += string.Format("{0}:{1},", attrObj.Title, value); } } //new } else if (newObj != null && oldObj != null) { result = string.Format("成功修改{0}!", action.ToString()); var props = typeof(T).GetProperties(); var idprop = props.FirstOrDefault(o => o.Name.ToUpper() == "ID"); var id = idprop.GetValue(newObj, null); result += string.Format("{0}Id:{1}{2}", Environment.NewLine, id, Environment.NewLine); foreach (var item in props) { var attr = item.GetCustomAttributes(typeof(OperationLogAttribute), true).FirstOrDefault(); if (attr != null) { var attrObj = (attr as OperationLogAttribute); object value = item.GetValue(newObj, null); object oldValue = item.GetValue(oldObj, null); bool isEquals = false; if (value == null) { isEquals = value != oldValue; } else { isEquals = !value.Equals(oldValue); } if (isEquals) { if (attrObj.IsDict) { switch (attrObj.Source) { case DictSource.InDBSysDict: SysDataDictionaryDAL _dal = new SysDataDictionaryDAL(); _dal.GetExtModelByDicSN((int)value); break; case DictSource.Prop: var titleProp = props.FirstOrDefault(o => o.Name == attrObj.Prop); if (titleProp != null) { value = titleProp.GetValue(newObj, null); } break; case DictSource.Enum: var enumtitle = attrObj.ShowDicts.FirstOrDefault(o => o.Contains(value.ToString().ToLower() + ":")); if (enumtitle != null) { value = enumtitle.Split(':')[1]; } break; } } result += string.Format("{0}:{1},", attrObj.Title, value); } } } //update } else { result = string.Format("成功删除{0}!", action.ToString()); var props = typeof(T).GetProperties(); var idprop = props.FirstOrDefault(o => o.Name.ToUpper() == "ID"); var id = idprop.GetValue(oldObj, null); result += string.Format("{0}Id:{1}{2}", Environment.NewLine, id, Environment.NewLine); foreach (var item in props) { var attr = item.GetCustomAttributes(typeof(OperationLogAttribute), true).FirstOrDefault(); if (attr != null) { var attrObj = (attr as OperationLogAttribute); object value = item.GetValue(oldObj, null); if (attrObj.IsDict) { switch (attrObj.Source) { case DictSource.InDBSysDict: SysDataDictionaryDAL _dal = new SysDataDictionaryDAL(); _dal.GetExtModelByDicSN((int)value); break; case DictSource.Prop: var titleProp = props.FirstOrDefault(o => o.Name == attrObj.Prop); if (titleProp != null) { value = titleProp.GetValue(oldObj, null); } break; case DictSource.Enum: var enumtitle = attrObj.ShowDicts.FirstOrDefault(o => o.Contains(value.ToString().ToLower() + ":")); if (enumtitle != null) { value = enumtitle.Split(':')[1]; } break; } } result += string.Format("{0}:{1},", attrObj.Title, value); } } } return(result); }
/// <summary> /// Writes a string to the log. /// </summary> /// <param name="module">The owner module.</param> /// <param name="level">Log level.</param> /// <param name="text">The string to write.</param> private static void Write(LogModule module, LogLevel level, string text) { lock (RvLogger.theLock) { if (RvLogger._Writer != null && RvLogger.IsPrintable(level)) { using (StringReader reader = new StringReader(text)) { string line; while ((line = reader.ReadLine()) != null) RvLogger._Writer.WriteLine("{0} | {1,9} | {2,11} | {3}", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.ff"), level.ToString(), module.ToString(), line); } RvLogger._Writer.Flush(); } } }
/// <summary> /// Formats the log message. /// </summary> /// <param name="module">The module.</param> /// <param name="message">The message.</param> /// <returns></returns> private static string FormatLogMessage(LogModule module, string message) { return(string.Format("{0} : {1}", module.ToString(), message)); }