using Newtonsoft.Json;
using ServiceCenter.Extensions;
using ServiceCenter.Redis;
using System.Collections.Concurrent;
namespace ServiceCenter
{
public class LogHub
{
///
/// 日志队列
///
public static ConcurrentQueue Logs = new ConcurrentQueue();
///
/// 发布一条日志记录
///
/// 路径
/// 文件名
/// 内容
public static void Publish(string path, string title, string con)
{
RedisHub.Default.RPush("LogHub", JsonConvert.SerializeObject(new LogModel
{
path = path,
Title = title,
Con = con
}));
}
///
/// 增加一条处理日志
///
/// 文件名
/// 内容
public static void InterfaceProcessLog(string title, string con)
{
Logs.Enqueue(new LogModel
{
path = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\接口处理日志",
Title = $"{title}.txt",
Con = con
});
}
public static void init()
{
while (true)
{
var log = JsonConvert.DeserializeObject(RedisHub.Default.LPop("LogHub"));
if (log != null)
{
if (!Directory.Exists(log.path)) Directory.CreateDirectory(log.path);
File.AppendAllText(Path.Combine(log.path, log.Title), log.Con);
}
}
}
}
public class LogModel
{
public string path { get; set; }
public string Title { get; set; }
public string Con { get; set; }
}
}