| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- using ivf_tl_Entity.Enums;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ivf_tl_Service
- {
- public class LogService
- {
- public string Pan { get; set; } = "C";
- private string logDir = "ivf_tl_Manage_logs";
- private object WriteRunRecordLock = new object();
- private object WriteRunErrorLock = new object();
- private object WriteRunHttpLock = new object();
- private object WriteRunMqttLock = 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 WriteRunKafkaLock = new object();
- /// <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;
- }
- }
- 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)}";
- WriteFile(filename, wroteContent);
- //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)}";
- WriteFile(filename, wroteContent);
- //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}";
- WriteFile(filename, wroteContent);
- //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 = LogDirectory;
- lock (WriteRunRecordLock)
- {
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- string filename = Path.Combine(path, "RunRecord.log");
- string wroteContent = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}] {recordTime.ToString("yyyy-MM-dd HH:mm:ss:fff")} {content}";
- WriteFile(filename, wroteContent);
- //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}";
- WriteFile(filename, wroteContent);
- //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}";
- WriteFile(filename, wroteContent);
- //using (StreamWriter mySw = File.AppendText(filename))
- //{
- // mySw.WriteLine(wroteContent);
- // mySw.Close();
- //}
- }
- }
- catch (Exception ex)
- {
- }
- }
- 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}";
- WriteFile(filename, wroteContent);
- //using (StreamWriter mySw = File.AppendText(filename))
- //{
- // mySw.WriteLine(wroteContent);
- // mySw.Close();
- //}
- }
- }
- catch (Exception ex)
- {
- }
- }
- private void WriteFile(string filename,string writeContent)
- {
- using (StreamWriter mySw = File.AppendText(filename))
- {
- mySw.WriteLine(writeContent);
- mySw.Close();
- }
- }
- }
- }
|