using IvfTl.Control.Entity.GlobalEnums; using IvfTl.Control.Services; using Newtonsoft.Json; namespace ivf_tl_ServicesImpl.LogServices { public class LogServiceImpl : ILogService { public string Pan { get; set; } = "C"; private string logDir = "ivf_tl_Control_logs"; /// /// RunException DbException /// public void ExceptionLog(Exception ex, string name, string canshu, LogEnum type) { switch (type) { case LogEnum.RunException: RunExceptionChuLi(ex, name, canshu, DateTime.Now); break; case LogEnum.DbException: DbExceptionChuLi(ex, name, canshu, DateTime.Now); break; } } /// /// PortComRecord HouseComRecord HouseInfo HouseRunRecord /// public void HouseLog(int houseId, DateTime date, string content, LogEnum type) { PortModelChuLi(houseId, date, content, type); } /// /// RunError、RunRecord、HttpClient、MqttClient、KafkaRecord /// /// /// public void TLLog(string writeString, LogEnum logType) { switch (logType) { case LogEnum.RunError: WriteRunErrorLog(DateTime.Now, writeString); break; case LogEnum.RunRecord: WriteRunRecordLog(DateTime.Now, writeString); break; case LogEnum.HttpClient: WriteRunHttpLog(DateTime.Now, writeString); break; case LogEnum.MqttClient: WriteRunMqttLog(DateTime.Now, writeString); break; case LogEnum.KafkaRecord: WriteRunKafkaLog(DateTime.Now, writeString); break; } } private object WriteRunRecordLock = new object(); private object WriteRunErrorLock = new object(); private object WriteRunExceptionLock = new object(); private object WriteDbExceptionLock = new object(); private object PortComRecordLock = new object(); private object HouseComRecordLock = new object(); private object HouseInfoLogLock = new object(); private object HouseRunRecordLock = new object(); private object WriteRunHttpLock = new object(); private object WriteRunMqttLock = new object(); private object WriteRunKafkaLock = new object(); /// /// 串口日志处理 HouseComRecord PortComRecord HouseInfo HouseRunRecord /// /// private void PortModelChuLi(int houseId, DateTime date, string content, LogEnum type) { switch (type) { case LogEnum.PortComRecord: PortComRecordLog(houseId, date, content); break; case LogEnum.HouseComRecord: HouseComRecordLog(houseId, date, content); break; case LogEnum.HouseInfo: WriteHouseInfoLog(houseId, date, content); break; case LogEnum.HouseRunRecord: HouseRunRecordLog(houseId, date, content); break; } } /// /// 运行异常处理 RunException /// /// /// /// private void RunExceptionChuLi(Exception ex, string name, string canshu, DateTime recorcdTime) { string body = null; try { body = JsonConvert.SerializeObject(ex); } catch (Exception) { if (ex.InnerException != null) { body = $"{ex.Message};{ex.InnerException.Message};{Environment.NewLine}详细异常:{ex.InnerException.StackTrace}"; } else { body = $"{ex.Message};{ex.StackTrace}"; } } if (canshu == null) { WriteRunExceptionLog(recorcdTime, $"{name}异常:{body}"); } else { WriteRunExceptionLog(recorcdTime, $"{name}异常:{body}{Environment.NewLine}参数:{canshu}"); } return; if (ex.InnerException != null) { WriteRunExceptionLog(recorcdTime, $"{name}异常详细:{ex.InnerException.Message}{ex.InnerException.StackTrace}"); } if (canshu == null) { WriteRunExceptionLog(recorcdTime, $"{name}异常:{ex.Message}{ex.StackTrace}"); } else { WriteRunExceptionLog(recorcdTime, $"{name}异常:{ex.Message}{ex.StackTrace}{Environment.NewLine}参数:{canshu}"); } } /// /// 数据库异常处理 DbException /// /// /// /// private void DbExceptionChuLi(Exception ex, string name, string canshu, DateTime recorcdTime) { string body = null; try { body = JsonConvert.SerializeObject(ex); } catch (Exception) { if (ex.InnerException != null) { body = $"{ex.Message};{ex.InnerException.Message};{Environment.NewLine}详细异常:{ex.InnerException.StackTrace}"; } else { body = $"{ex.Message};{ex.StackTrace}"; } } if (canshu == null) { WriteDbExceptionLog(recorcdTime, $"{name}异常:{body}"); } else { WriteDbExceptionLog(recorcdTime, $"{name}异常:{body}{Environment.NewLine}参数:{canshu}"); } return; } /// /// 记录程序运行情况 RunRecord /// private void WriteRunRecordLog(DateTime recordTime, string content) { try { string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\"; string dirTime = recordTime.ToString("yyyy-MM-dd"); string path = Path.Combine(LogDirectory, dirTime); lock (WriteRunRecordLock) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filename = Path.Combine(path, $"RunRecord-{dirTime}.log"); string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}"; using (StreamWriter mySw = File.AppendText(filename)) { mySw.WriteLine(wroteContent); mySw.Close(); } } } catch (Exception ex) { } } /// /// 记录接口访问情况 /// private void WriteRunHttpLog(DateTime recordTime, string content) { try { string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\"; string dirTime = recordTime.ToString("yyyy-MM-dd"); string path = Path.Combine(LogDirectory, dirTime); lock (WriteRunHttpLock) { if (!Directory.Exists(path)) Directory.CreateDirectory(path); string filename = Path.Combine(path, $"HttpRecord-{dirTime}.log"); string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}"; using (StreamWriter mySw = File.AppendText(filename)) { mySw.WriteLine(wroteContent); mySw.Close(); } } } catch (Exception ex) { } } /// /// mqtt消息记录 /// private void WriteRunMqttLog(DateTime recordTime, string content) { try { string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\"; string dirTime = recordTime.ToString("yyyy-MM-dd"); string path = Path.Combine(LogDirectory, dirTime); lock (WriteRunMqttLock) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filename = Path.Combine(path, $"MqttRecord-{dirTime}.log"); string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}"; using (StreamWriter mySw = File.AppendText(filename)) { mySw.WriteLine(wroteContent); mySw.Close(); } } } catch (Exception ex) { } } /// /// kafka消息记录 /// private void WriteRunKafkaLog(DateTime recordTime, string content) { try { string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\"; string dirTime = recordTime.ToString("yyyy-MM-dd"); string path = Path.Combine(LogDirectory, dirTime); lock (WriteRunKafkaLock) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filename = Path.Combine(path, $"KafkaRecord-{dirTime}.log"); string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}"; using (StreamWriter mySw = File.AppendText(filename)) { mySw.WriteLine(wroteContent); mySw.Close(); } } } catch (Exception ex) { } } /// /// 记录程序错误 RunError /// /// /// private void WriteRunErrorLog(DateTime recordTime, string content) { try { string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\"; string dirTime = recordTime.ToString("yyyy-MM-dd"); string path = Path.Combine(LogDirectory, dirTime); lock (WriteRunErrorLock) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filename = Path.Combine(path, $"RunError-{dirTime}.log"); string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}"; using (StreamWriter mySw = File.AppendText(filename)) { mySw.WriteLine(wroteContent); mySw.Close(); } } } catch (Exception ex) { } } /// /// 记录程序异常 /// /// /// private void WriteRunExceptionLog(DateTime recordTime, string content) { try { string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\"; string dirTime = recordTime.ToString("yyyy-MM-dd"); string path = Path.Combine(LogDirectory, dirTime); lock (WriteRunExceptionLock) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filename = Path.Combine(path, $"RunException-{dirTime}.log"); string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}"; using (StreamWriter mySw = File.AppendText(filename)) { mySw.WriteLine(wroteContent); mySw.Close(); } } } catch (Exception ex) { } } /// /// 记录数据库异常 /// private void WriteDbExceptionLog(DateTime recordTime, string content) { try { string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\"; string dirTime = recordTime.ToString("yyyy-MM-dd"); string path = Path.Combine(LogDirectory, dirTime); lock (WriteDbExceptionLock) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filename = Path.Combine(path, $"DbException-{dirTime}.log"); string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}"; using (StreamWriter mySw = File.AppendText(filename)) { mySw.WriteLine(wroteContent); mySw.Close(); } } } catch (Exception ex) { //ExceptionChuLi(ex, "WriteDbExceptionLog", $"[date:{recordTime.ToString("yyyy-MM-dd")}][content:{content}]"); } } /// /// 串口通信记录 /// private void PortComRecordLog(int houseId, DateTime date, string content) { try { string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\"; string dirTime = date.ToString("yyyy-MM-dd"); string path = Path.Combine(LogDirectory, dirTime); lock (PortComRecordLock) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filename = Path.Combine(path, $"house{houseId}-PortComRecord-{dirTime}.log"); string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {date.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}"; using (StreamWriter mySw = File.AppendText(filename)) { mySw.WriteLine(wroteContent); mySw.Close(); } } } catch (Exception ex) { //ExceptionChuLi(ex, "PortComRecordLog", $"[houseId:{houseId}][date:{date.ToString("yyyy-MM-dd")}][content:{content}]"); } } /// /// 仓室通信记录 /// private void HouseComRecordLog(int houseId, DateTime date, string content) { try { string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\"; string dirTime = date.ToString("yyyy-MM-dd"); string path = Path.Combine(LogDirectory, dirTime); lock (HouseComRecordLock) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filename = Path.Combine(path, $"house{houseId}-HouseComRecord-{dirTime}.log"); string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {date.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}"; using (StreamWriter mySw = File.AppendText(filename)) { mySw.WriteLine(wroteContent); mySw.Close(); } } } catch (Exception ex) { //ExceptionChuLi(ex, "HouseComRecordLog", $"[houseId:{houseId}][date:{date.ToString("yyyy-MM-dd")}][content:{content}]"); } } /// /// 舱室信息日志写入 /// /// /// private void WriteHouseInfoLog(int houseId, DateTime date, string content) { try { string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\"; string dirTime = date.ToString("yyyy-MM-dd"); string path = Path.Combine(LogDirectory, dirTime); lock (HouseInfoLogLock) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filename = Path.Combine(path, $"house{houseId}-{dirTime}.log"); string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {date.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}"; using (StreamWriter mySw = File.AppendText(filename)) { mySw.WriteLine(wroteContent); mySw.Close(); } } } catch (Exception ex) { //ExceptionChuLi(ex, "WriteHouseInfoLog", $"[houseId:{houseId}][date:{date.ToString("yyyy-MM-dd")}][content:{content}]"); } } /// /// 仓室运行记录 /// /// /// /// private void HouseRunRecordLog(int houseId, DateTime date, string content) { try { string LogDirectory = $"{Pan}:\\TLData\\{logDir}\\"; string dirTime = date.ToString("yyyy-MM-dd"); string path = Path.Combine(LogDirectory, dirTime); lock (HouseRunRecordLock) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filename = Path.Combine(path, $"house{houseId}-HouseRunRecord-{dirTime}.log"); string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {date.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}"; using (StreamWriter mySw = File.AppendText(filename)) { mySw.WriteLine(wroteContent); mySw.Close(); } } } catch (Exception ex) { //ExceptionChuLi(ex, "HouseComRecordLog", $"[houseId:{houseId}][date:{date.ToString("yyyy-MM-dd")}][content:{content}]"); } } } }