using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Web; using System.Text.RegularExpressions; using System.Linq; using System.Collections; using System.Reflection; using System.Collections.Specialized; namespace MJTop.Data { /// /// 日志操作类 /// internal class LogUtils { private static object locker = new object(); /// /// 获取请求相关信息 /// /// 日志级别 /// private static List GetRequestData(LogLevel level) { List lstdata = new List(); return lstdata; } /// /// 写入日志 /// /// 日志名称 /// 开发记录者 /// 日志级别 /// 日志详情 /// 记录时间 public static void Write(string logName, Developer developer, LogLevel level, string detail, DateTime createtime) { Log log = new Log(); log.LogName = logName; log.Level = level; log.Developer = developer; log.CreateTime = createtime; List lstDetails = GetRequestData(level); lstDetails.Add(detail); log.Detail = string.Join("\r\n\r\n", lstDetails.ToArray()); //todo :可以将日志写入 文件、数据库、MongoDB //这里写入根目录 log文件夹 string logText = Log.GetModelData(log) + "\r\n----------------------------------------------------------------------------------------------------\r\n"; string fileName = logName + DateTime.Now.ToString("yyyyMMdd") + ".log"; string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log"); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } fileName = Path.Combine(dir, fileName); File.AppendAllText(fileName, logText, Encoding.UTF8); } /// /// 写入Info 日志 /// /// 日志名称 /// 开发记录者 /// 日志内容 public static void LogInfo(string logName, Developer developer, params object[] Info_objs) { lock (locker) { List lstDetails = new List(); if (Info_objs != null && Info_objs.Length > 0) { List lstInfo = new List(); foreach (var item in Info_objs) { lstInfo.Add(Log.GetModelData(item)); } lstDetails.Add("标记信息:" + string.Join(";", lstInfo.ToArray())); } Write(logName, developer, LogLevel.Info, string.Join("\r\n", lstDetails.ToArray()), DateTime.Now); } } /// s /// 写入带 堆栈执行 的Info 日志 /// /// 日志名称 /// 开发记录者 /// 日志内容 public static void LogWrite(string logName, Developer developer, params object[] Info_objs) { lock (locker) { List lstDetails = new List(); System.Diagnostics.StackTrace stack = new System.Diagnostics.StackTrace(1, true); System.Diagnostics.StackFrame frame = stack.GetFrame(0); string execFile = frame.GetFileName(); string fullName = frame.GetMethod().DeclaringType.FullName; string methodName = frame.GetMethod().Name; int execLine = frame.GetFileLineNumber(); lstDetails.Add("文件路径:" + execFile + "\r\n"); lstDetails.Add("类全命名:" + fullName + "\r\n"); lstDetails.Add("执行方法:" + methodName + "\r\n"); lstDetails.Add("当前行号:" + execLine + "\r\n"); if (Info_objs != null && Info_objs.Length > 0) { List lstInfo = new List(); foreach (var item in Info_objs) { lstInfo.Add(Log.GetModelData(item)); } lstDetails.Add("标记信息:" + string.Join(";", lstInfo.ToArray())); } Write(logName, developer, LogLevel.Info, string.Join("\r\n", lstDetails.ToArray()), DateTime.Now); } } /// /// 写入Warn 日志 /// /// 日志名称 /// 开发记录者 /// 日志内容 public static void LogWarn(string logName, Developer developer, params object[] Info_objs) { lock (locker) { List lstDetails = new List(); System.Diagnostics.StackTrace stack = new System.Diagnostics.StackTrace(1, true); System.Diagnostics.StackFrame frame = stack.GetFrame(0); string execFile = frame.GetFileName(); string fullName = frame.GetMethod().DeclaringType.FullName; string methodName = frame.GetMethod().Name; int execLine = frame.GetFileLineNumber(); lstDetails.Add("文件路径:" + execFile + "\r\n"); lstDetails.Add("类全命名:" + fullName + "\r\n"); lstDetails.Add("执行方法:" + methodName + "\r\n"); lstDetails.Add("当前行号:" + execLine + "\r\n"); if (Info_objs != null && Info_objs.Length > 0) { List lstInfo = new List(); foreach (var item in Info_objs) { lstInfo.Add(Log.GetModelData(item)); } lstDetails.Add("标记信息:" + string.Join(";", lstInfo.ToArray())); } Write(logName, developer, LogLevel.Warn, string.Join("\r\n", lstDetails.ToArray()), DateTime.Now); } } /// /// 写入 Errorr日志 /// /// 日志名称 /// 开发记录者 /// 异常对象(可为null) /// 日志内容 public static void LogError(string logName, Developer developer, Exception ex, params object[] ext_InfoObjs) { lock (locker) { List lstDetails = new List(); lstDetails.Add("异常信息1:" + Log.GetModelData(ex)); if (ex.InnerException != null) { lstDetails.Add("异常信息2:" + Log.GetModelData(ex.InnerException)); } StringBuilder sb_extInfo = new StringBuilder(); if (ext_InfoObjs != null && ext_InfoObjs.Length > 0) { List lst_ext_Inf = new List(); foreach (var item in ext_InfoObjs) { lst_ext_Inf.Add(Log.GetModelData(item)); } lstDetails.Add("标记信息:" + string.Join(";", lst_ext_Inf.ToArray())); } string detail = string.Join("\r\n\r\n", lstDetails.ToArray()); Write(logName, developer, LogLevel.Error, detail, DateTime.Now); } } } /// /// 程序日志 /// public class Log { public Guid Id { get { return Guid.NewGuid(); } } /// /// 日志名称 /// public string LogName { get; set; } /// /// 日志级别 /// public LogLevel Level { get; set; } /// /// 当前记录日志者 /// public Developer Developer { get; set; } /// /// 日志详细内容 /// public string Detail { get; set; } /// /// 日志时间 /// public DateTime CreateTime { get; set; } #region private 反射 对象 /// /// 得到对象的所有属性值 /// /// 对象 /// public static string GetModelData(object obj) { string valueParam = string.Empty; StringBuilder sb = new StringBuilder(); if (obj == null || string.IsNullOrEmpty(obj.ToString())) { return string.Empty; } Type objType = obj.GetType(); if (IsSimpleType(objType)) { valueParam = obj.ToString(); } else if (obj is NameValueCollection) { valueParam = GetCollectionData(obj as ICollection); } else { PropertyInfo[] proInfos = objType.GetProperties(); foreach (PropertyInfo proInfo in proInfos) { string name = proInfo.Name; object objvalue = null; string value = string.Empty; try { objvalue = proInfo.GetValue(obj, null); } catch { } if (objvalue == null) { value = string.Empty; } else { value = objvalue.ToString(); } sb.AppendLine(name + ":" + value + "\r\n"); } valueParam = sb.ToString().TrimEnd(); } return valueParam; } /// /// 得到集合 数组中所有值 /// /// 集合对象 /// public static string GetCollectionData(ICollection obj) { if (obj == null || string.IsNullOrEmpty(obj.ToString())) { return string.Empty; } string valueParam = string.Empty; Type objType = obj.GetType(); string typeName = objType.Name; Type[] argumentsTypes = objType.GetGenericArguments(); #region isLstMark isDictMark bool isLstMark = false; if (argumentsTypes.Length == 1) { if (IsSimpleType(argumentsTypes[0])) { isLstMark = true; } } else { isLstMark = (obj as IList) != null; } bool isDictMark = false; if (argumentsTypes.Length == 2) { if (IsSimpleType(argumentsTypes[0]) && IsSimpleType(argumentsTypes[1])) { isDictMark = true; } } else { isDictMark = ((obj as IDictionary) != null); } #endregion if (objType.IsArray) { #region 数组类型 int arrRank = objType.GetArrayRank(); if (arrRank == 1) { Array arr = (Array)obj; if (arr != null && arr.LongLength > 0) { List lst = new List(); foreach (var item in arr) { if (item != null) { lst.Add(item.ToString()); } } valueParam = string.Join(",", lst.ToArray()); } } #endregion } else if (isLstMark) { #region List IEnumerable enumlst = obj as IEnumerable; if (enumlst != null) { List lsts = new List(); foreach (var item in enumlst) { if (item != null) { lsts.Add(item.ToString()); } } if (lsts.Count > 0) { valueParam = string.Join(",", lsts.ToArray()); } } #endregion } else if (isDictMark) { #region Dictionary IDictionary dict = obj as IDictionary; if (dict != null && dict.Count > 0) { StringBuilder sb = new StringBuilder(); foreach (DictionaryEntry item in dict) { sb.AppendLine(item.Key + ":" + item.Value + "\r\n"); } valueParam = sb.ToString(); } #endregion } else if (obj is NameValueCollection) { #region NameValueCollection NameValueCollection nvc = (NameValueCollection)obj; if (nvc != null && nvc.Count > 0) { StringBuilder sb = new StringBuilder(); foreach (string key in nvc.AllKeys) { sb.AppendLine(key + ":" + nvc[key] + "\r\n"); } valueParam = sb.ToString(); } #endregion } else if (obj is ICollection) { #region ICollection ICollection coll = obj as ICollection; if (coll != null) { List lstObjs = new List(); foreach (var item in coll) { if (item != null) { lstObjs.Add(item.ToString()); } } if (lstObjs.Count > 0) { valueParam = string.Join(",", lstObjs.ToArray()); } } #endregion } return valueParam.TrimEnd(); } public static bool IsSimpleType(Type type) { //IsPrimitive 判断是否为基础类型。 //基元类型为 Boolean、 Byte、 SByte、 Int16、 UInt16、 Int32、 UInt32、 Int64、 UInt64、 IntPtr、 UIntPtr、 Char、 Double 和 Single。 Type t = Nullable.GetUnderlyingType(type) ?? type; if (t.IsPrimitive || t.IsEnum || t == typeof(string)) return true; return false; } #endregion #region 枚举 处理 /// /// 根据枚举对象得到 枚举键值对 /// /// 枚举 /// public static Dictionary GetAllEnums() { Dictionary dict = null; Type type = typeof(T); string[] enums = Enum.GetNames(type); if (enums != null && enums.Length > 0) { dict = new Dictionary(); foreach (string item in enums) { string str = Enum.Parse(typeof(T), item).ToString(); T deve = (T)Enum.Parse(typeof(T), item); string uid = Convert.ToInt32(deve).ToString(); dict.Add(str, uid); } } return dict; } /// /// 根据枚举val获取枚举name /// /// 枚举类型 /// 枚举val /// 枚举name public static T GetEnumName(int enumVal) { T t = (T)Enum.Parse(typeof(T), enumVal.ToString()); return t; } #endregion } /// /// 日志级别 /// public enum LogLevel { Info = 0, Warn = 1, Error = 2 } /// /// 日志记录开发者 /// public enum Developer { /// /// 系统默认 /// SysDefault = 0, /// /// 其他用户 /// MJ = 115 } }