LogManager.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace FileManager
  11. {
  12. public class LogManager
  13. {
  14. #region 定义属性
  15. private static string filePath = string.Empty;
  16. /// <summary>
  17. /// 存储路径
  18. /// </summary>
  19. protected static string FilePath { get => filePath; set => filePath = value; }
  20. /// <summary>
  21. /// 运行状态
  22. /// </summary>
  23. private static bool runState = false;
  24. protected static bool RunState { get => runState; set => runState = value; }
  25. #endregion
  26. #region 初始化创建日志文件
  27. /// <summary>
  28. /// 初始化日志文件,如不存在根据xml路径创建
  29. /// </summary>
  30. /// <param name="xmlFilePath">xml存储位置</param>
  31. public static void Init(string xmlFilePath)
  32. {
  33. CreateFile(xmlFilePath);
  34. }
  35. #endregion
  36. #region 添加记录日志
  37. /// <summary>
  38. /// 记录运行日志
  39. /// </summary>
  40. /// <param name="logContent">日志运行内容</param>
  41. public static void LogTrace(string logContent)
  42. {
  43. //获取调用当前方法的页面与代码行数
  44. StackTrace st = new StackTrace(new StackFrame(1, true));
  45. StackFrame sf = st.GetFrame(0);
  46. //文件名称
  47. string FileName = sf.GetFileName();
  48. //方法名
  49. string MethodName = sf.GetMethod().Name;
  50. //行号
  51. int LineNumber = sf.GetFileLineNumber();
  52. //设置运行状态
  53. RunState = true;
  54. st = null;
  55. LogWrite(FileName, MethodName, LineNumber, logContent, RunState);
  56. }
  57. /// <summary>
  58. /// 记录错误日志
  59. /// </summary>
  60. /// <param name="logErrorContent">日志错误内容</param>
  61. public static void LogError(string logErrorContent)
  62. {
  63. //获取调用当前方法的页面与代码行数
  64. StackTrace st = new StackTrace(new StackFrame(1, true));
  65. StackFrame sf = st.GetFrame(0);
  66. //文件名称
  67. string FileName = sf.GetFileName();
  68. //方法名
  69. string MethodName = sf.GetMethod().Name;
  70. //行号
  71. int LineNumber = sf.GetFileLineNumber();
  72. //设置运行状态
  73. RunState = false;
  74. st = null;
  75. LogWrite(FileName, MethodName, LineNumber, logErrorContent, RunState);
  76. }
  77. #endregion
  78. #region 读取日志信息 返回DataTable
  79. /// <summary>
  80. /// 读取日志信息 返回DataTable
  81. /// </summary>
  82. /// <param name="xmlFilePath">xml路径</param>
  83. /// <returns>返回DataTable</returns>
  84. public static DataTable LogRead(string xmlFilePath)
  85. {
  86. try
  87. {
  88. if (xmlFilePath.Equals(""))
  89. {
  90. return null;
  91. }
  92. int lastIndex = xmlFilePath.LastIndexOf(@"/");
  93. string path = xmlFilePath.Substring(0, lastIndex);
  94. //设置测量文件的路径
  95. FilePath = path + @"/log.txt";
  96. //读取日志信息
  97. FileStream fsRead = new FileStream(FilePath, FileMode.Open, FileAccess.Read);//创建写入文件
  98. int fsLen = (int)fsRead.Length;
  99. byte[] heByte = new byte[fsLen];
  100. int r = fsRead.Read(heByte, 0, heByte.Length);
  101. //将信息转换为字符串
  102. string logStr = System.Text.Encoding.UTF8.GetString(heByte);
  103. fsRead.Close();
  104. DataTable dt = CreateDataSource(logStr);
  105. return dt;
  106. }
  107. catch (Exception)
  108. {
  109. return null;
  110. }
  111. }
  112. /// <summary>
  113. /// 根据日志内容创建数据源
  114. /// </summary>
  115. /// <param name="logStr"></param>
  116. /// <returns></returns>
  117. protected static DataTable CreateDataSource(string logStr)
  118. {
  119. //拆分为字符组
  120. string[] groupStr = logStr.Split('\n');
  121. //创建数据源
  122. DataTable dt = new DataTable();
  123. dt.Columns.Add("切入点名称");
  124. dt.Columns.Add("步骤名称");
  125. dt.Columns.Add("完成时间");
  126. dt.Columns.Add("状态");
  127. foreach (string item in groupStr)
  128. {
  129. if (!item.Equals(""))
  130. {
  131. DataRow dataRow = dt.NewRow();
  132. //将每个字符组拆分为对应的信息
  133. string[] eleStr = item.Split('-');
  134. //切入点名称与步骤信息
  135. string[] strNameCotent = eleStr[5].Split(':');
  136. if (strNameCotent.Length > 1)
  137. {
  138. dataRow[0] = strNameCotent[0];
  139. dataRow[1] = strNameCotent[1];
  140. }
  141. else
  142. {
  143. dataRow[0] = eleStr[5];
  144. dataRow[1] = eleStr[5];
  145. }
  146. dataRow[2] = eleStr[4];
  147. dataRow[3] = eleStr[6];
  148. dt.Rows.Add(dataRow);
  149. }
  150. }
  151. return dt;
  152. }
  153. #endregion
  154. #region 日志文件类操作
  155. /// <summary>
  156. /// 创建日志文件
  157. /// </summary>
  158. protected static bool CreateFile(string xmlFilePath)
  159. {
  160. if (xmlFilePath.Equals(""))
  161. {
  162. return false;
  163. }
  164. //拆分xml路径
  165. int lastIndex = xmlFilePath.LastIndexOf(@"/");
  166. string path = xmlFilePath.Substring(0, lastIndex);
  167. //设置日志文件的路径
  168. FilePath = path+@"/log.txt";
  169. //判断是否存在日志路径
  170. if (!IsExistFile(filePath))
  171. {
  172. //没有则创建这个文件
  173. FileStream fs1 = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  174. fs1.Close();
  175. return true;
  176. }
  177. return false;
  178. }
  179. /// <summary>
  180. /// 判断是否存在日志文件
  181. /// </summary>
  182. protected static bool IsExistFile(string filePath)
  183. {
  184. if (File.Exists(filePath))
  185. {
  186. return true;
  187. }
  188. return false;
  189. }
  190. /// <summary>
  191. /// 写日志
  192. /// </summary>
  193. protected static void LogWrite(string FileName, string MethodName, int LineNumber, string logContent,bool logState)
  194. {
  195. try
  196. {
  197. //记录日志时间
  198. string currTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
  199. //格式:(日志类型+日志记录的所在文件名称+方法名称+行号+时间+描述+状态)
  200. string logType = "Trace";
  201. if (!logState)
  202. {
  203. logType = "Error";
  204. }
  205. //日志类型:LogType:Trace 文件名:xxxx.cs 方法名:aaa() 行数:10 时间:2020-XX-XX XX:XX:XX 描述:切点名称-操作描述 状态:True
  206. string logStr = "LogType:" + logType + "-" + FileName + "-" + MethodName + "-" + LineNumber + "-" + currTime + "-" + logContent + "-" + logState.ToString() + "";
  207. //写入日志信息
  208. FileStream fs1 = new FileStream(filePath, FileMode.Append, FileAccess.Write);//创建写入文件
  209. StreamWriter sw = new StreamWriter(fs1);
  210. sw.WriteLine(logStr);//开始写入值
  211. sw.Close();
  212. fs1.Close();
  213. }
  214. catch (Exception)
  215. {
  216. return;
  217. }
  218. }
  219. #endregion
  220. }
  221. }