LogServiceImpl.cs 21 KB

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