LogHelper.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. using ivf_tl_Entity.GlobalEnums;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection.Metadata;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace ivf_tl_Services
  11. {
  12. public class LogHelper
  13. {
  14. public string Pan { get; set; } = "C";
  15. private string logDir = "ivf_tl_Operate_logs";
  16. private object WriteRunRecordLock = new object();
  17. private object WriteRunErrorLock = new object();
  18. private object WriteRunExceptionLock = new object();
  19. private object WriteDbExceptionLock = new object();
  20. private object PortComRecordLock = new object();
  21. private object HouseComRecordLock = new object();
  22. private object HouseInfoLogLock = new object();
  23. private object HouseRunRecordLock = new object();
  24. private object WriteRunHttpLock = new object();
  25. private object WriteRunMqttLock = new object();
  26. private object WriteRunKafkaLock = new object();
  27. /// <summary>
  28. /// PortComRecord HouseComRecord HouseInfo HouseRunRecord
  29. /// </summary>
  30. public void HouseLog(int houseId, DateTime date, string content, LogEnum type)
  31. {
  32. PortModelChuLi(houseId, date, content, type);
  33. }
  34. /// <summary>
  35. /// RunError、RunRecord、HttpClient、MqttClient
  36. /// </summary>
  37. /// <param name="writeString"></param>
  38. /// <param name="logType"></param>
  39. public void TLLog(string writeString, LogEnum logType)
  40. {
  41. switch (logType)
  42. {
  43. case LogEnum.RunError:
  44. WriteRunErrorLog(DateTime.Now, writeString);
  45. break;
  46. case LogEnum.RunRecord:
  47. WriteRunRecordLog(DateTime.Now, writeString);
  48. break;
  49. case LogEnum.HttpClient:
  50. WriteRunHttpLog(DateTime.Now, writeString);
  51. break;
  52. case LogEnum.MqttClient:
  53. WriteRunMqttLog(DateTime.Now, writeString);
  54. break;
  55. case LogEnum.TakeTime:
  56. WriteTimeLog(DateTime.Now, writeString);
  57. break;
  58. }
  59. }
  60. /// <summary>
  61. /// RunException DbException
  62. /// </summary>
  63. public void ExceptionLog(Exception ex, string name, LogEnum type)
  64. {
  65. switch (type)
  66. {
  67. case LogEnum.RunException:
  68. RunExceptionChuLi(ex, name, DateTime.Now);
  69. break;
  70. case LogEnum.DbException:
  71. DbExceptionChuLi(ex, name, DateTime.Now);
  72. break;
  73. }
  74. }
  75. /// <summary>
  76. /// 数据库异常处理
  77. /// </summary>
  78. /// <param name="ex"></param>
  79. /// <param name="name"></param>
  80. /// <param name="recordTime"></param>
  81. private void DbExceptionChuLi(Exception ex, string name, DateTime recordTime)
  82. {
  83. try
  84. {
  85. string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\";
  86. string dirTime = recordTime.ToString("yyyy-MM-dd");
  87. string path = Path.Combine(LogDirectory, dirTime);
  88. lock (WriteDbExceptionLock)
  89. {
  90. if (!Directory.Exists(path))
  91. {
  92. Directory.CreateDirectory(path);
  93. }
  94. string filename = Path.Combine(path, $"DbException-{dirTime}.log");
  95. string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {name}异常:{JsonConvert.SerializeObject(ex)}";
  96. using (StreamWriter mySw = File.AppendText(filename))
  97. {
  98. mySw.WriteLine(wroteContent);
  99. mySw.Close();
  100. }
  101. }
  102. }
  103. catch (Exception ex1)
  104. {
  105. }
  106. }
  107. /// <summary>
  108. /// 运行异常处理
  109. /// </summary>
  110. /// <param name="ex"></param>
  111. /// <param name="name"></param>
  112. /// <param name="recordTime"></param>
  113. private void RunExceptionChuLi(Exception ex, string name, DateTime recordTime)
  114. {
  115. try
  116. {
  117. string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\";
  118. string dirTime = recordTime.ToString("yyyy-MM-dd");
  119. string path = Path.Combine(LogDirectory, dirTime);
  120. lock (WriteRunExceptionLock)
  121. {
  122. if (!Directory.Exists(path))
  123. {
  124. Directory.CreateDirectory(path);
  125. }
  126. string filename = Path.Combine(path, $"RunException-{dirTime}.log");
  127. string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {name}异常:{JsonConvert.SerializeObject(ex)}";
  128. using (StreamWriter mySw = File.AppendText(filename))
  129. {
  130. mySw.WriteLine(wroteContent);
  131. mySw.Close();
  132. }
  133. }
  134. }
  135. catch (Exception ex1)
  136. {
  137. }
  138. }
  139. /// <summary>
  140. /// 记录程序错误 RunError
  141. /// </summary>
  142. /// <param name="recordTime"></param>
  143. /// <param name="content"></param>
  144. private void WriteRunErrorLog(DateTime recordTime, string content)
  145. {
  146. try
  147. {
  148. string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\";
  149. string dirTime = recordTime.ToString("yyyy-MM-dd");
  150. string path = Path.Combine(LogDirectory, dirTime);
  151. lock (WriteRunErrorLock)
  152. {
  153. if (!Directory.Exists(path))
  154. {
  155. Directory.CreateDirectory(path);
  156. }
  157. string filename = Path.Combine(path, $"RunError-{dirTime}.log");
  158. string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}";
  159. using (StreamWriter mySw = File.AppendText(filename))
  160. {
  161. mySw.WriteLine(wroteContent);
  162. mySw.Close();
  163. }
  164. }
  165. }
  166. catch (Exception ex)
  167. {
  168. }
  169. }
  170. /// <summary>
  171. /// 记录程序运行情况 RunRecord
  172. /// </summary>
  173. private void WriteRunRecordLog(DateTime recordTime, string content)
  174. {
  175. try
  176. {
  177. string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\";
  178. string dirTime = recordTime.ToString("yyyy-MM-dd");
  179. string path = Path.Combine(LogDirectory, dirTime);
  180. lock (WriteRunRecordLock)
  181. {
  182. if (!Directory.Exists(path))
  183. {
  184. Directory.CreateDirectory(path);
  185. }
  186. string filename = Path.Combine(path, $"RunRecord-{dirTime}.log");
  187. string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}";
  188. using (StreamWriter mySw = File.AppendText(filename))
  189. {
  190. mySw.WriteLine(wroteContent);
  191. mySw.Close();
  192. }
  193. }
  194. }
  195. catch (Exception ex)
  196. {
  197. }
  198. }
  199. /// <summary>
  200. /// 记录接口访问情况
  201. /// </summary>
  202. private void WriteRunHttpLog(DateTime recordTime, string content)
  203. {
  204. try
  205. {
  206. string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\";
  207. string dirTime = recordTime.ToString("yyyy-MM-dd");
  208. string path = Path.Combine(LogDirectory, dirTime);
  209. lock (WriteRunHttpLock)
  210. {
  211. if (!Directory.Exists(path))
  212. {
  213. Directory.CreateDirectory(path);
  214. }
  215. string filename = Path.Combine(path, $"HttpRecord-{dirTime}.log");
  216. string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}";
  217. using (StreamWriter mySw = File.AppendText(filename))
  218. {
  219. mySw.WriteLine(wroteContent);
  220. mySw.Close();
  221. }
  222. }
  223. }
  224. catch (Exception ex)
  225. {
  226. }
  227. }
  228. /// <summary>
  229. /// mqtt消息记录
  230. /// </summary>
  231. private void WriteRunMqttLog(DateTime recordTime, string content)
  232. {
  233. try
  234. {
  235. string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\";
  236. string dirTime = recordTime.ToString("yyyy-MM-dd");
  237. string path = Path.Combine(LogDirectory, dirTime);
  238. lock (WriteRunMqttLock)
  239. {
  240. if (!Directory.Exists(path))
  241. {
  242. Directory.CreateDirectory(path);
  243. }
  244. string filename = Path.Combine(path, $"MqttRecord-{dirTime}.log");
  245. string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}";
  246. using (StreamWriter mySw = File.AppendText(filename))
  247. {
  248. mySw.WriteLine(wroteContent);
  249. mySw.Close();
  250. }
  251. }
  252. }
  253. catch (Exception ex)
  254. {
  255. }
  256. }
  257. /// <summary>
  258. /// 串口日志处理 HouseComRecord PortComRecord HouseInfo HouseRunRecord
  259. /// </summary>
  260. /// <param name="logModel"></param>
  261. private void PortModelChuLi(int houseId, DateTime date, string content, LogEnum type)
  262. {
  263. switch (type)
  264. {
  265. case LogEnum.PortComRecord:
  266. PortComRecordLog(houseId, date, content);
  267. break;
  268. case LogEnum.HouseComRecord:
  269. HouseComRecordLog(houseId, date, content);
  270. break;
  271. case LogEnum.HouseInfo:
  272. WriteHouseInfoLog(houseId, date, content);
  273. break;
  274. case LogEnum.HouseRunRecord:
  275. HouseRunRecordLog(houseId, date, content);
  276. break;
  277. }
  278. }
  279. /// <summary>
  280. /// 串口通信记录
  281. /// </summary>
  282. private void PortComRecordLog(int houseId, DateTime date, string content)
  283. {
  284. try
  285. {
  286. string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\";
  287. string dirTime = date.ToString("yyyy-MM-dd");
  288. string path = Path.Combine(LogDirectory, dirTime);
  289. lock (PortComRecordLock)
  290. {
  291. if (!Directory.Exists(path))
  292. {
  293. Directory.CreateDirectory(path);
  294. }
  295. string filename = Path.Combine(path, $"house{houseId}-PortComRecord-{dirTime}.log");
  296. string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {date.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}";
  297. using (StreamWriter mySw = File.AppendText(filename))
  298. {
  299. mySw.WriteLine(wroteContent);
  300. mySw.Close();
  301. }
  302. }
  303. }
  304. catch (Exception ex)
  305. {
  306. }
  307. }
  308. /// <summary>
  309. /// 舱室通信记录
  310. /// </summary>
  311. private void HouseComRecordLog(int houseId, DateTime date, string content)
  312. {
  313. try
  314. {
  315. string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\";
  316. string dirTime = date.ToString("yyyy-MM-dd");
  317. string path = Path.Combine(LogDirectory, dirTime);
  318. lock (HouseComRecordLock)
  319. {
  320. if (!Directory.Exists(path))
  321. {
  322. Directory.CreateDirectory(path);
  323. }
  324. string filename = Path.Combine(path, $"house{houseId}-HouseComRecord-{dirTime}.log");
  325. string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {date.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}";
  326. using (StreamWriter mySw = File.AppendText(filename))
  327. {
  328. mySw.WriteLine(wroteContent);
  329. mySw.Close();
  330. }
  331. }
  332. }
  333. catch (Exception ex)
  334. {
  335. //ExceptionChuLi(ex, "HouseComRecordLog", $"[houseId:{houseId}][date:{date.ToString("yyyy-MM-dd")}][content:{content}]");
  336. }
  337. }
  338. /// <summary>
  339. /// 舱室信息日志写入
  340. /// </summary>
  341. /// <param name="type"></param>
  342. /// <param name="content"></param>
  343. private void WriteHouseInfoLog(int houseId, DateTime date, string content)
  344. {
  345. try
  346. {
  347. string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\";
  348. string dirTime = date.ToString("yyyy-MM-dd");
  349. string path = Path.Combine(LogDirectory, dirTime);
  350. lock (HouseInfoLogLock)
  351. {
  352. if (!Directory.Exists(path))
  353. {
  354. Directory.CreateDirectory(path);
  355. }
  356. string filename = Path.Combine(path, $"house{houseId}-{dirTime}.log");
  357. string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {date.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}";
  358. using (StreamWriter mySw = File.AppendText(filename))
  359. {
  360. mySw.WriteLine(wroteContent);
  361. mySw.Close();
  362. }
  363. }
  364. }
  365. catch (Exception ex)
  366. {
  367. //ExceptionChuLi(ex, "WriteHouseInfoLog", $"[houseId:{houseId}][date:{date.ToString("yyyy-MM-dd")}][content:{content}]");
  368. }
  369. }
  370. /// <summary>
  371. /// 舱室运行记录
  372. /// </summary>
  373. /// <param name="houseId"></param>
  374. /// <param name="date"></param>
  375. /// <param name="content"></param>
  376. private void HouseRunRecordLog(int houseId, DateTime date, string content)
  377. {
  378. try
  379. {
  380. string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\";
  381. string dirTime = date.ToString("yyyy-MM-dd");
  382. string path = Path.Combine(LogDirectory, dirTime);
  383. lock (HouseRunRecordLock)
  384. {
  385. if (!Directory.Exists(path))
  386. {
  387. Directory.CreateDirectory(path);
  388. }
  389. string filename = Path.Combine(path, $"house{houseId}-HouseRunRecord-{dirTime}.log");
  390. string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {date.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}";
  391. using (StreamWriter mySw = File.AppendText(filename))
  392. {
  393. mySw.WriteLine(wroteContent);
  394. mySw.Close();
  395. }
  396. }
  397. }
  398. catch (Exception ex)
  399. {
  400. //ExceptionChuLi(ex, "HouseComRecordLog", $"[houseId:{houseId}][date:{date.ToString("yyyy-MM-dd")}][content:{content}]");
  401. }
  402. }
  403. private void WriteTimeLog(DateTime recordTime, string content)
  404. {
  405. try
  406. {
  407. string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\";
  408. string dirTime = recordTime.ToString("yyyy-MM-dd");
  409. string path = Path.Combine(LogDirectory, dirTime);
  410. lock (WriteRunMqttLock)
  411. {
  412. if (!Directory.Exists(path))
  413. {
  414. Directory.CreateDirectory(path);
  415. }
  416. string filename = Path.Combine(path, $"TakeTime-{dirTime}.log");
  417. string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}";
  418. using (StreamWriter mySw = File.AppendText(filename))
  419. {
  420. mySw.WriteLine(wroteContent);
  421. mySw.Close();
  422. }
  423. }
  424. }
  425. catch (Exception ex)
  426. {
  427. }
  428. }
  429. #region 路径管理
  430. /// <summary>
  431. /// 调试模式单张抓拍目录
  432. /// </summary>
  433. /// <returns></returns>
  434. public string GetDeBugDanZhangDirectory(int HouseId, int well)
  435. {
  436. string DanZhangDirectory = $"{Pan}:\\TLData\\DebugPictures\\house{HouseId}\\单张抓拍\\well{well}\\";
  437. return DanZhangDirectory;
  438. }
  439. /// <summary>
  440. /// 调试模式水平抓图目录
  441. /// </summary>
  442. /// <returns></returns>
  443. public string GetDeBugShuiPingDirectory(int HouseId, string dtnow)
  444. {
  445. string ShuiPingDirectory = $"{Pan}:\\TLData\\DebugPictures\\house{HouseId}\\水平抓图\\{dtnow}\\";
  446. return ShuiPingDirectory;
  447. }
  448. /// <summary>
  449. /// 调试模式清晰图层抓图目录
  450. /// </summary>
  451. /// <returns></returns>
  452. public string GetDeBugQingXiDirectory(int HouseId, int well, string dtnow)
  453. {
  454. string QingXiDirectory = $"{Pan}:\\TLData\\DebugPictures\\house{HouseId}\\清晰图层抓图\\well{well}\\{dtnow}\\";
  455. return QingXiDirectory;
  456. }
  457. #endregion
  458. }
  459. }