| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- 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();
- /// <summary>
- /// PortComRecord HouseComRecord HouseInfo HouseRunRecord
- /// </summary>
- public void HouseLog(int houseId, DateTime date, string content, LogEnum type)
- {
- PortModelChuLi(houseId, date, content, type);
- }
- /// <summary>
- /// RunError、RunRecord、HttpClient、MqttClient
- /// </summary>
- /// <param name="writeString"></param>
- /// <param name="logType"></param>
- 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;
- }
- }
- /// <summary>
- /// RunException DbException
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 数据库异常处理
- /// </summary>
- /// <param name="ex"></param>
- /// <param name="name"></param>
- /// <param name="recordTime"></param>
- 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)
- {
- }
- }
- /// <summary>
- /// 运行异常处理
- /// </summary>
- /// <param name="ex"></param>
- /// <param name="name"></param>
- /// <param name="recordTime"></param>
- 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)
- {
- }
- }
- /// <summary>
- /// 记录程序错误 RunError
- /// </summary>
- /// <param name="recordTime"></param>
- /// <param name="content"></param>
- 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)
- {
- }
- }
- /// <summary>
- /// 记录程序运行情况 RunRecord
- /// </summary>
- 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)
- {
- }
- }
- /// <summary>
- /// 记录接口访问情况
- /// </summary>
- 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)
- {
- }
- }
- /// <summary>
- /// mqtt消息记录
- /// </summary>
- 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)
- {
- }
- }
- /// <summary>
- /// 串口日志处理 HouseComRecord PortComRecord HouseInfo HouseRunRecord
- /// </summary>
- /// <param name="logModel"></param>
- 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;
- }
- }
- /// <summary>
- /// 串口通信记录
- /// </summary>
- 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)
- {
- }
- }
- /// <summary>
- /// 舱室通信记录
- /// </summary>
- 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}]");
- }
- }
- /// <summary>
- /// 舱室信息日志写入
- /// </summary>
- /// <param name="type"></param>
- /// <param name="content"></param>
- 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}]");
- }
- }
- /// <summary>
- /// 舱室运行记录
- /// </summary>
- /// <param name="houseId"></param>
- /// <param name="date"></param>
- /// <param name="content"></param>
- 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 路径管理
- /// <summary>
- /// 调试模式单张抓拍目录
- /// </summary>
- /// <returns></returns>
- public string GetDeBugDanZhangDirectory(int HouseId, int well)
- {
- string DanZhangDirectory = $"{Pan}:\\TLData\\DebugPictures\\house{HouseId}\\单张抓拍\\well{well}\\";
- return DanZhangDirectory;
- }
- /// <summary>
- /// 调试模式水平抓图目录
- /// </summary>
- /// <returns></returns>
- public string GetDeBugShuiPingDirectory(int HouseId, string dtnow)
- {
- string ShuiPingDirectory = $"{Pan}:\\TLData\\DebugPictures\\house{HouseId}\\水平抓图\\{dtnow}\\";
- return ShuiPingDirectory;
- }
- /// <summary>
- /// 调试模式清晰图层抓图目录
- /// </summary>
- /// <returns></returns>
- public string GetDeBugQingXiDirectory(int HouseId, int well, string dtnow)
- {
- string QingXiDirectory = $"{Pan}:\\TLData\\DebugPictures\\house{HouseId}\\清晰图层抓图\\well{well}\\{dtnow}\\";
- return QingXiDirectory;
- }
- #endregion
- }
- }
|