LogManager.cs 12 KB

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