using Newtonsoft.Json;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Net.Http.Json;
|
using System.Reflection.Metadata;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace WIDESEA_Comm.LogInfo
|
{
|
/// <summary>
|
/// 日志写入
|
/// </summary>
|
public partial class WriteLog
|
{
|
/// <summary>
|
/// 写入日志(不要多线程操作同一文件,一个设备一个文件夹)
|
/// </summary>
|
public static void Write_Log(string groupName, string logName, string content, object data = null)
|
{
|
string basePath = System.Environment.CurrentDirectory + "/Log/" + $"/{groupName}/{DateTime.Now.ToString("yyyy_MM_dd")}";
|
//如果日志文件目录不存在,则创建
|
if (!Directory.Exists(basePath))
|
{
|
Directory.CreateDirectory(basePath);
|
}
|
try
|
{
|
FileStream fs = new FileStream(basePath + "/" + logName + $"{DateTime.Now.ToString("yyyy_MM_dd")}_Log.txt", FileMode.Append);
|
StreamWriter strwriter = new StreamWriter(fs);
|
try
|
{
|
DateTime d = DateTime.Now;
|
strwriter.WriteLine(d.ToString());
|
strwriter.WriteLine(content);
|
if (data != null)
|
{
|
strwriter.WriteLine(JsonConvert.SerializeObject(data));
|
}
|
strwriter.WriteLine("-------------------------------");
|
strwriter.WriteLine();
|
strwriter.Flush();
|
}
|
catch { }
|
finally
|
{
|
strwriter.Close();
|
fs.Close();
|
}
|
}
|
catch { }
|
}
|
}
|
}
|