LogService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using ivf_tl_Entity.Enums;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace ivf_tl_Service
  10. {
  11. public class LogService
  12. {
  13. public string Pan { get; set; } = "C";
  14. private string logDir = "ivf_tl_Manage_logs";
  15. private object WriteRunRecordLock = new object();
  16. private object WriteRunErrorLock = new object();
  17. private object WriteRunHttpLock = new object();
  18. private object WriteRunMqttLock = new object();
  19. private object WriteRunExceptionLock = new object();
  20. private object WriteDbExceptionLock = new object();
  21. private object PortComRecordLock = new object();
  22. private object HouseComRecordLock = new object();
  23. private object HouseInfoLogLock = new object();
  24. private object HouseRunRecordLock = new object();
  25. private object WriteRunKafkaLock = new object();
  26. /// <summary>
  27. /// RunError、RunRecord、HttpClient、MqttClient
  28. /// </summary>
  29. /// <param name="writeString"></param>
  30. /// <param name="logType"></param>
  31. public void TLLog(string writeString, LogEnum logType)
  32. {
  33. switch (logType)
  34. {
  35. case LogEnum.RunError:
  36. WriteRunErrorLog(DateTime.Now, writeString);
  37. break;
  38. case LogEnum.RunRecord:
  39. WriteRunRecordLog(DateTime.Now, writeString);
  40. break;
  41. case LogEnum.HttpClient:
  42. WriteRunHttpLog(DateTime.Now, writeString);
  43. break;
  44. case LogEnum.MqttClient:
  45. WriteRunMqttLog(DateTime.Now, writeString);
  46. break;
  47. case LogEnum.TakeTime:
  48. WriteTimeLog(DateTime.Now, writeString);
  49. break;
  50. }
  51. }
  52. public void ExceptionLog(Exception ex, string name, LogEnum type)
  53. {
  54. switch (type)
  55. {
  56. case LogEnum.RunException:
  57. RunExceptionChuLi(ex, name, DateTime.Now);
  58. break;
  59. case LogEnum.DbException:
  60. DbExceptionChuLi(ex, name, DateTime.Now);
  61. break;
  62. }
  63. }
  64. /// <summary>
  65. /// 数据库异常处理
  66. /// </summary>
  67. /// <param name="ex"></param>
  68. /// <param name="name"></param>
  69. /// <param name="recordTime"></param>
  70. private void DbExceptionChuLi(Exception ex, string name, DateTime recordTime)
  71. {
  72. try
  73. {
  74. string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\";
  75. string dirTime = recordTime.ToString("yyyy-MM-dd");
  76. string path = Path.Combine(LogDirectory, dirTime);
  77. lock (WriteDbExceptionLock)
  78. {
  79. if (!Directory.Exists(path))
  80. {
  81. Directory.CreateDirectory(path);
  82. }
  83. string filename = Path.Combine(path, $"DbException-{dirTime}.log");
  84. 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)}";
  85. WriteFile(filename, wroteContent);
  86. //using (StreamWriter mySw = File.AppendText(filename))
  87. //{
  88. // mySw.WriteLine(wroteContent);
  89. // mySw.Close();
  90. //}
  91. }
  92. }
  93. catch (Exception ex1)
  94. {
  95. }
  96. }
  97. /// <summary>
  98. /// 运行异常处理
  99. /// </summary>
  100. /// <param name="ex"></param>
  101. /// <param name="name"></param>
  102. /// <param name="recordTime"></param>
  103. private void RunExceptionChuLi(Exception ex, string name, DateTime recordTime)
  104. {
  105. try
  106. {
  107. string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\";
  108. string dirTime = recordTime.ToString("yyyy-MM-dd");
  109. string path = Path.Combine(LogDirectory, dirTime);
  110. lock (WriteRunExceptionLock)
  111. {
  112. if (!Directory.Exists(path))
  113. {
  114. Directory.CreateDirectory(path);
  115. }
  116. string filename = Path.Combine(path, $"RunException-{dirTime}.log");
  117. 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)}";
  118. WriteFile(filename, wroteContent);
  119. //using (StreamWriter mySw = File.AppendText(filename))
  120. //{
  121. // mySw.WriteLine(wroteContent);
  122. // mySw.Close();
  123. //}
  124. }
  125. }
  126. catch (Exception ex1)
  127. {
  128. }
  129. }
  130. /// <summary>
  131. /// 记录程序错误 RunError
  132. /// </summary>
  133. /// <param name="recordTime"></param>
  134. /// <param name="content"></param>
  135. private void WriteRunErrorLog(DateTime recordTime, string content)
  136. {
  137. try
  138. {
  139. string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\";
  140. string dirTime = recordTime.ToString("yyyy-MM-dd");
  141. string path = Path.Combine(LogDirectory, dirTime);
  142. lock (WriteRunErrorLock)
  143. {
  144. if (!Directory.Exists(path))
  145. {
  146. Directory.CreateDirectory(path);
  147. }
  148. string filename = Path.Combine(path, $"RunError-{dirTime}.log");
  149. string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}";
  150. WriteFile(filename, wroteContent);
  151. //using (StreamWriter mySw = File.AppendText(filename))
  152. //{
  153. // mySw.WriteLine(wroteContent);
  154. // mySw.Close();
  155. //}
  156. }
  157. }
  158. catch (Exception ex)
  159. {
  160. }
  161. }
  162. /// <summary>
  163. /// 记录程序运行情况 RunRecord
  164. /// </summary>
  165. private void WriteRunRecordLog(DateTime recordTime, string content)
  166. {
  167. try
  168. {
  169. string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\";
  170. string dirTime = recordTime.ToString("yyyy-MM-dd");
  171. string path = LogDirectory;
  172. lock (WriteRunRecordLock)
  173. {
  174. if (!Directory.Exists(path))
  175. {
  176. Directory.CreateDirectory(path);
  177. }
  178. string filename = Path.Combine(path, "RunRecord.log");
  179. string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}";
  180. WriteFile(filename, wroteContent);
  181. //using (StreamWriter mySw = File.AppendText(filename))
  182. //{
  183. // mySw.WriteLine(wroteContent);
  184. // mySw.Close();
  185. //}
  186. }
  187. }
  188. catch (Exception ex)
  189. {
  190. }
  191. }
  192. /// <summary>
  193. /// 记录接口访问情况
  194. /// </summary>
  195. private void WriteRunHttpLog(DateTime recordTime, string content)
  196. {
  197. try
  198. {
  199. string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\";
  200. string dirTime = recordTime.ToString("yyyy-MM-dd");
  201. string path = Path.Combine(LogDirectory, dirTime);
  202. lock (WriteRunHttpLock)
  203. {
  204. if (!Directory.Exists(path))
  205. {
  206. Directory.CreateDirectory(path);
  207. }
  208. string filename = Path.Combine(path, $"HttpRecord-{dirTime}.log");
  209. string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}";
  210. WriteFile(filename, wroteContent);
  211. //using (StreamWriter mySw = File.AppendText(filename))
  212. //{
  213. // mySw.WriteLine(wroteContent);
  214. // mySw.Close();
  215. //}
  216. }
  217. }
  218. catch (Exception ex)
  219. {
  220. }
  221. }
  222. /// <summary>
  223. /// mqtt消息记录
  224. /// </summary>
  225. private void WriteRunMqttLog(DateTime recordTime, string content)
  226. {
  227. try
  228. {
  229. string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\";
  230. string dirTime = recordTime.ToString("yyyy-MM-dd");
  231. string path = Path.Combine(LogDirectory, dirTime);
  232. lock (WriteRunMqttLock)
  233. {
  234. if (!Directory.Exists(path))
  235. {
  236. Directory.CreateDirectory(path);
  237. }
  238. string filename = Path.Combine(path, $"MqttRecord-{dirTime}.log");
  239. string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}";
  240. WriteFile(filename, wroteContent);
  241. //using (StreamWriter mySw = File.AppendText(filename))
  242. //{
  243. // mySw.WriteLine(wroteContent);
  244. // mySw.Close();
  245. //}
  246. }
  247. }
  248. catch (Exception ex)
  249. {
  250. }
  251. }
  252. private void WriteTimeLog(DateTime recordTime, string content)
  253. {
  254. try
  255. {
  256. string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\";
  257. string dirTime = recordTime.ToString("yyyy-MM-dd");
  258. string path = Path.Combine(LogDirectory, dirTime);
  259. lock (WriteRunMqttLock)
  260. {
  261. if (!Directory.Exists(path))
  262. {
  263. Directory.CreateDirectory(path);
  264. }
  265. string filename = Path.Combine(path, $"TakeTime-{dirTime}.log");
  266. string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}";
  267. WriteFile(filename, wroteContent);
  268. //using (StreamWriter mySw = File.AppendText(filename))
  269. //{
  270. // mySw.WriteLine(wroteContent);
  271. // mySw.Close();
  272. //}
  273. }
  274. }
  275. catch (Exception ex)
  276. {
  277. }
  278. }
  279. private void WriteFile(string filename,string writeContent)
  280. {
  281. using (StreamWriter mySw = File.AppendText(filename))
  282. {
  283. mySw.WriteLine(writeContent);
  284. mySw.Close();
  285. }
  286. }
  287. }
  288. }