LogManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. using System.Windows.Forms;
  11. namespace FileManager
  12. {
  13. public class LogManager
  14. {
  15. #region 定义属性
  16. private static string filePath = string.Empty;
  17. /// <summary>
  18. /// 存储路径
  19. /// </summary>
  20. protected static string FilePath { get => filePath; set => filePath = value; }
  21. /// <summary>
  22. /// 运行状态
  23. /// </summary>
  24. private static bool runState = false;
  25. protected static bool RunState { get => runState; set => runState = value; }
  26. /// <summary>
  27. /// 日志名称,按照当前时间生成
  28. /// </summary>
  29. protected string LogName { get => logName; set => logName = value; }
  30. /// <summary>
  31. /// 硬件日志文件路径
  32. /// </summary>
  33. protected static string HardwareLogPath { get => hardwareLogPath; set => hardwareLogPath = value; }
  34. //默认日志路径
  35. private static string defaultPath = @"\Log\";
  36. //硬件日志文件路径
  37. private static string hardwareLogPath = string.Empty;
  38. //硬件日志名称
  39. private static string logName = string.Empty;
  40. #endregion
  41. #region 初始化创建日志文件
  42. /// <summary>
  43. /// 初始化日志文件,如不存在根据xml路径创建
  44. /// </summary>
  45. /// <param name="xmlFilePath">xml存储位置</param>
  46. public static void Init(string xmlFilePath)
  47. {
  48. CreateFile(xmlFilePath);
  49. }
  50. /// <summary>
  51. /// 初始化记录硬件操作日志文件,默认创建日志文件
  52. /// </summary>
  53. public static void InitHardwareLog()
  54. {
  55. //硬件日志名称
  56. logName = DateTime.Now.ToString("yyyyMMddHHmmss")+ "HardwareLog.txt";
  57. CreateHardwareFile(logName);
  58. }
  59. #endregion
  60. #region 添加记录日志
  61. /// <summary>
  62. /// 记录运行日志
  63. /// </summary>
  64. /// <param name="logContent">日志运行内容</param>
  65. public static void LogTrace(string logContent)
  66. {
  67. //获取调用当前方法的页面与代码行数
  68. StackTrace st = new StackTrace(new StackFrame(1, true));
  69. StackFrame sf = st.GetFrame(0);
  70. //文件名称
  71. string FileName = sf.GetFileName();
  72. //方法名
  73. string MethodName = sf.GetMethod().Name;
  74. //行号
  75. int LineNumber = sf.GetFileLineNumber();
  76. //设置运行状态
  77. RunState = true;
  78. st = null;
  79. LogWrite(FileName, MethodName, LineNumber, logContent, RunState);
  80. }
  81. /// <summary>
  82. /// 记录错误日志
  83. /// </summary>
  84. /// <param name="logErrorContent">日志错误内容</param>
  85. public static void LogError(string logErrorContent)
  86. {
  87. //获取调用当前方法的页面与代码行数
  88. StackTrace st = new StackTrace(new StackFrame(1, true));
  89. StackFrame sf = st.GetFrame(0);
  90. //文件名称
  91. string FileName = sf.GetFileName();
  92. //方法名
  93. string MethodName = sf.GetMethod().Name;
  94. //行号
  95. int LineNumber = sf.GetFileLineNumber();
  96. //设置运行状态
  97. RunState = false;
  98. st = null;
  99. LogWrite(FileName, MethodName, LineNumber, logErrorContent, RunState);
  100. }
  101. /// <summary>
  102. /// 添加硬件运行日志
  103. /// </summary>
  104. /// <param name="logContent">日志内容</param>
  105. /// <param name="logState">True:正常日志 False:错误日志</param>
  106. public static void AddHardwareLog(string logContent,bool logState)
  107. {
  108. //获取调用当前方法的页面与代码行数
  109. StackTrace st = new StackTrace(new StackFrame(1, true));
  110. StackFrame sf = st.GetFrame(0);
  111. //文件名称
  112. string FileName = sf.GetFileName();
  113. //方法名
  114. string MethodName = sf.GetMethod().Name;
  115. //行号
  116. int LineNumber = sf.GetFileLineNumber();
  117. ////设置运行状态
  118. //RunState = true;
  119. st = null;
  120. HardwareLogWrite(FileName, MethodName, LineNumber, logContent, logState);
  121. }
  122. #endregion
  123. #region 读取日志信息 返回DataTable
  124. /// <summary>
  125. /// 读取日志信息 返回DataTable
  126. /// </summary>
  127. /// <param name="xmlFilePath">xml路径</param>
  128. /// <returns>返回DataTable</returns>
  129. public static DataTable LogRead(string xmlFilePath)
  130. {
  131. try
  132. {
  133. if (xmlFilePath.Equals(""))
  134. {
  135. return null;
  136. }
  137. int lastIndex = xmlFilePath.LastIndexOf(@"/");
  138. string path = xmlFilePath.Substring(0, lastIndex);
  139. //设置测量文件的路径
  140. FilePath = path + @"/log.txt";
  141. //读取日志信息
  142. FileStream fsRead = new FileStream(FilePath, FileMode.Open, FileAccess.Read);//创建写入文件
  143. int fsLen = (int)fsRead.Length;
  144. byte[] heByte = new byte[fsLen];
  145. int r = fsRead.Read(heByte, 0, heByte.Length);
  146. //将信息转换为字符串
  147. string logStr = System.Text.Encoding.UTF8.GetString(heByte);
  148. fsRead.Close();
  149. DataTable dt = CreateDataSource(logStr);
  150. return dt;
  151. }
  152. catch (Exception)
  153. {
  154. return null;
  155. }
  156. }
  157. /// <summary>
  158. /// 根据日志内容创建数据源
  159. /// </summary>
  160. /// <param name="logStr"></param>
  161. /// <returns></returns>
  162. protected static DataTable CreateDataSource(string logStr)
  163. {
  164. //拆分为字符组
  165. string[] groupStr = logStr.Split('\n');
  166. //创建数据源
  167. DataTable dt = new DataTable();
  168. dt.Columns.Add("切入点名称");
  169. dt.Columns.Add("步骤名称");
  170. dt.Columns.Add("完成时间");
  171. dt.Columns.Add("状态");
  172. foreach (string item in groupStr)
  173. {
  174. if (!item.Equals(""))
  175. {
  176. DataRow dataRow = dt.NewRow();
  177. //将每个字符组拆分为对应的信息
  178. string[] eleStr = item.Split('-');
  179. //切入点名称与步骤信息
  180. string[] strNameCotent = eleStr[5].Split(':');
  181. if (strNameCotent.Length > 1)
  182. {
  183. dataRow[0] = strNameCotent[0];
  184. dataRow[1] = strNameCotent[1];
  185. }
  186. else
  187. {
  188. dataRow[0] = eleStr[5];
  189. dataRow[1] = eleStr[5];
  190. }
  191. dataRow[2] = eleStr[4];
  192. dataRow[3] = eleStr[6];
  193. dt.Rows.Add(dataRow);
  194. }
  195. }
  196. return dt;
  197. }
  198. #endregion
  199. #region 日志文件类操作
  200. /// <summary>
  201. /// 创建日志文件
  202. /// </summary>
  203. protected static bool CreateFile(string xmlFilePath)
  204. {
  205. if (xmlFilePath.Equals(""))
  206. {
  207. return false;
  208. }
  209. //拆分xml路径
  210. int lastIndex = xmlFilePath.LastIndexOf(@"/");
  211. string path = xmlFilePath.Substring(0, lastIndex);
  212. //设置日志文件的路径
  213. FilePath = path+@"/log.txt";
  214. //判断是否存在日志路径
  215. if (!IsExistFile(filePath))
  216. {
  217. //没有则创建这个文件
  218. FileStream fs1 = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  219. fs1.Close();
  220. return true;
  221. }
  222. return false;
  223. }
  224. protected static bool CreateHardwareFile(string logName)
  225. {
  226. string DirectoryPath = Application.StartupPath + defaultPath;
  227. if (!Directory.Exists(DirectoryPath))
  228. {
  229. Directory.CreateDirectory(DirectoryPath);
  230. }
  231. HardwareLogPath = DirectoryPath + logName;
  232. //判断是否存在日志路径
  233. if (!IsExistFile(HardwareLogPath))
  234. {
  235. //没有则创建这个文件
  236. FileStream fs1 = new FileStream(HardwareLogPath, FileMode.Create, FileAccess.Write);
  237. fs1.Close();
  238. return true;
  239. }
  240. return false;
  241. }
  242. /// <summary>
  243. /// 判断是否存在日志文件
  244. /// </summary>
  245. protected static bool IsExistFile(string filePath)
  246. {
  247. if (File.Exists(filePath))
  248. {
  249. return true;
  250. }
  251. return false;
  252. }
  253. /// <summary>
  254. /// 写日志
  255. /// </summary>
  256. protected static void LogWrite(string FileName, string MethodName, int LineNumber, string logContent,bool logState)
  257. {
  258. try
  259. {
  260. //记录日志时间
  261. string currTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
  262. //格式:(日志类型+日志记录的所在文件名称+方法名称+行号+时间+描述+状态)
  263. string logType = "Trace";
  264. if (!logState)
  265. {
  266. logType = "Error";
  267. }
  268. //日志类型:LogType:Trace 文件名:xxxx.cs 方法名:aaa() 行数:10 时间:2020-XX-XX XX:XX:XX 描述:切点名称-操作描述 状态:True
  269. string logStr = "LogType:" + logType + "-" + FileName + "-" + MethodName + "-" + LineNumber + "-" + currTime + "-" + logContent + "-" + logState.ToString() + "";
  270. //写入日志信息
  271. FileStream fs1 = new FileStream(filePath, FileMode.Append, FileAccess.Write);//创建写入文件
  272. StreamWriter sw = new StreamWriter(fs1);
  273. sw.WriteLine(logStr);//开始写入值
  274. sw.Close();
  275. fs1.Close();
  276. }
  277. catch (Exception)
  278. {
  279. return;
  280. }
  281. }
  282. protected static void HardwareLogWrite(string FileName, string MethodName, int LineNumber, string logContent, bool logState)
  283. {
  284. try
  285. {
  286. //记录日志时间
  287. string currTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
  288. //格式:(日志类型+日志记录的所在文件名称+方法名称+行号+时间+描述+状态)
  289. string logType = "Trace";
  290. if (!logState)
  291. {
  292. logType = "Error";
  293. }
  294. //日志类型:LogType:Trace 文件名:xxxx.cs 方法名:aaa() 行数:10 时间:2020-XX-XX XX:XX:XX 描述:切点名称-操作描述 状态:True
  295. string logStr = "LogType:" + logType + "-" + FileName + "-" + MethodName + "-" + LineNumber + "-" + currTime + "-" + logContent + "-" + logState.ToString() + "";
  296. //写入日志信息
  297. FileStream fs1 = new FileStream(HardwareLogPath, FileMode.Append, FileAccess.Write);//创建写入文件
  298. StreamWriter sw = new StreamWriter(fs1);
  299. sw.WriteLine(logStr);//开始写入值
  300. sw.Close();
  301. fs1.Close();
  302. }
  303. catch (Exception)
  304. {
  305. return;
  306. }
  307. }
  308. #endregion
  309. }
  310. }