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();
///
/// 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;
}
}
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)}";
WriteFile(filename, wroteContent);
//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)}";
WriteFile(filename, wroteContent);
//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}";
WriteFile(filename, wroteContent);
//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 = 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)
{
}
}
///
/// 记录接口访问情况
///
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)
{
}
}
///
/// 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}";
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();
}
}
}
}