using ivf_tl_Entity.GlobalEnums; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; namespace ivf_tl_Services { public class LogHelper { public string Pan { get; set; } = "C"; private string logDir = "ivf_tl_Operate_logs"; 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(); /// /// PortComRecord HouseComRecord HouseInfo HouseRunRecord /// public void HouseLog(int houseId, DateTime date, string content, LogEnum type) { PortModelChuLi(houseId, date, content, type); } /// /// RunError、RunRecord、HttpClient、MqttClient /// /// /// 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.TakeTime: WriteTimeLog(DateTime.Now, writeString); break; } } /// /// RunException DbException /// public void ExceptionLog(Exception ex, string name, LogEnum type) { switch (type) { case LogEnum.RunException: RunExceptionChuLi(ex, name, DateTime.Now); break; case LogEnum.DbException: DbExceptionChuLi(ex, name, DateTime.Now); break; } } /// /// 数据库异常处理 /// /// /// /// private void DbExceptionChuLi(Exception ex, string name, DateTime recordTime) { 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")} {name}异常:{JsonConvert.SerializeObject(ex)}"; using (StreamWriter mySw = File.AppendText(filename)) { mySw.WriteLine(wroteContent); mySw.Close(); } } } catch (Exception ex1) { } } /// /// 运行异常处理 /// /// /// /// private void RunExceptionChuLi(Exception ex, string name, DateTime recordTime) { 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")} {name}异常:{JsonConvert.SerializeObject(ex)}"; using (StreamWriter mySw = File.AppendText(filename)) { mySw.WriteLine(wroteContent); mySw.Close(); } } } catch (Exception ex1) { } } /// /// 记录程序错误 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) { } } /// /// 记录程序运行情况 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) { } } /// /// 串口日志处理 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; } } /// /// 串口通信记录 /// 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) { } } /// /// 舱室通信记录 /// 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}]"); } } private void WriteTimeLog(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, $"TakeTime-{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) { } } #region 路径管理 /// /// 调试模式单张抓拍目录 /// /// public string GetDeBugDanZhangDirectory(int HouseId, int well) { string DanZhangDirectory = $"{Pan}:\\TLData\\DebugPictures\\house{HouseId}\\单张抓拍\\well{well}\\"; return DanZhangDirectory; } /// /// 调试模式水平抓图目录 /// /// public string GetDeBugShuiPingDirectory(int HouseId, string dtnow) { string ShuiPingDirectory = $"{Pan}:\\TLData\\DebugPictures\\house{HouseId}\\水平抓图\\{dtnow}\\"; return ShuiPingDirectory; } /// /// 调试模式清晰图层抓图目录 /// /// public string GetDeBugQingXiDirectory(int HouseId, int well, string dtnow) { string QingXiDirectory = $"{Pan}:\\TLData\\DebugPictures\\house{HouseId}\\清晰图层抓图\\well{well}\\{dtnow}\\"; return QingXiDirectory; } #endregion } }