|
|
|
using HDLCloudMonitorSupportCtrlOldUdpGate.Context;
|
using Newtonsoft.Json;
|
using System;
|
using System.Collections.Generic;
|
using System.IO;
|
|
namespace HDLCloudMonitorSupportCtrlOldUdpGate.Logs
|
{
|
public partial class MqttLogHp
|
{
|
private static Object lockObj = new Object();
|
|
public static void Save()
|
{
|
lock (lockObj)
|
{
|
ProcessLogDir();
|
string currentDataFilePath = $"~/LogDir/{GetCurrentTimeFormatStr()}.txt";
|
string content = JsonConvert.SerializeObject(GlobalRecordContext.CtrlRecordDto);
|
AppendTextToFile(currentDataFilePath, content, true);
|
}
|
}
|
|
|
public static List<CtrlRecordDto> ReadDataByDateHours(string dateHours)
|
{
|
var dataList = new List<CtrlRecordDto>();
|
|
string currentDataFilePath = MapPath($"~/LogDir/{dateHours}.txt");
|
if (File.Exists(currentDataFilePath))
|
{
|
string[] fileAllLines = File.ReadAllLines(currentDataFilePath);
|
foreach (var lineJson in fileAllLines)
|
{
|
dataList.Add(JsonConvert.DeserializeObject<CtrlRecordDto>(lineJson));
|
}
|
}
|
return dataList;
|
}
|
|
private static void ProcessLogDir()
|
{
|
string logDirPath = MapPath($"~/LogDir");
|
if (!Directory.Exists(logDirPath))
|
{
|
Directory.CreateDirectory(logDirPath);
|
}
|
}
|
|
private static string GetCurrentTimeFormatStr()
|
{
|
return DateTime.Now.ToString(Consts.DateTimeFormatHoursStr);
|
}
|
|
private static void AppendTextToFile(string path, string content, bool isAddRn = true)
|
{
|
path = MapPath(path);
|
|
if (isAddRn)
|
{
|
File.AppendAllText(path, content + "\r\n");
|
}
|
else
|
{
|
File.AppendAllText(path, content);
|
}
|
}
|
|
private static string MapPath(string path)
|
{
|
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
|
path = path.Replace("~/", "").TrimStart('/').Replace('/', '\\');
|
return Path.Combine(baseDirectory, path); //Path.Combine() 特别适合生成 aa\bb 类型目录
|
}
|
}
|
}
|