实体模型日志记录的两种方法

在接口交互过程中或一步完整的提交之前通常需要记录下Request、Response的业务数据信息,当然前提是你通过实体类的方式存储了数据,如果不想一个个字段的方式去拼写日志格式,也不愿意在实体类内定义属性以记录特定关键的日志,可以通过以下两种方法来操作,大家都知道的,废话不多说。

§ 序列化

这里采用框架自带的序列化,当然你也可以采用诸如Newtonsoft的序列化组件,但原生态的序列化在日期类型字段为默认值时序列化会报错。
        /// 
        /// 序列化
        /// 
        /// 
        /// 
        /// 
        /// 
        public static string Serialize(T obj, Encoding encoding)
        {
            string retVal = string.Empty;
            using (MemoryStream ms = new MemoryStream())
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                serializer.WriteObject(ms, obj);
                retVal = encoding.GetString(ms.ToArray());
            }
            return retVal;
        }

        /// 
        /// 反序列化
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T Deserialize(this string s, Encoding encoding) where T : class
        {
            //T obj = Activator.CreateInstance();
            using (MemoryStream ms = new MemoryStream(encoding.GetBytes(s)))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                return serializer.ReadObject(ms) as T;
            }
        }


§ 反射

private static Dictionary> ObjectCache = new Dictionary>();
        public static string GetObjectDetails(this object o, string spliter)
        {
            if (o == null)
            {
                return "NULL";
            }
            Type t = o.GetType();
            ICollection ic = ObjectCache;
            lock (ic.SyncRoot)
            {
                if (!ObjectCache.Keys.Contains(t))
                {
                    List mems = new List();
                    PropertyInfo[] pros = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                    foreach (PropertyInfo p in pros)
                    {
                        if (p.CanRead)
                        {
                            mems.Add(p);
                        }
                    }
                    FieldInfo[] fields = t.GetFields(BindingFlags.Public | BindingFlags.Instance);
                    foreach (FieldInfo f in fields)
                    {
                        mems.Add(f);
                    }
                    //KeyValuePair> kvp = new KeyValuePair>(t, mems);
                    ObjectCache.Add(t, mems);
                }
            }

            StringBuilder sb = new StringBuilder();
            ObjectCache[t].ForEach(tmp =>
            {
                FieldInfo f = tmp as FieldInfo;
                if (f != null)
                {
                    object valueObject = f.GetValue(o);
                    sb.Append(f.Name);
                    sb.Append(":    ");
                    sb.Append(valueObject);
                    sb.Append(spliter);
                }
                else
                {
                    PropertyInfo p = tmp as PropertyInfo;
                    if (p.CanRead)
                    {
                        sb.Append(p.Name);
                        sb.Append(":    ");
                        sb.Append(p.GetValue(o, null));
                        sb.Append(spliter);
                    }
                }
            });
            return sb.ToString();

        }

你可能感兴趣的:(Java)