12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- using Newtonsoft.Json;
- using ServiceCenter.Extensions;
- using ServiceCenter.Redis;
- using System.Collections.Concurrent;
- namespace ServiceCenter
- {
- public class LogHub
- {
- /// <summary>
- /// 日志队列
- /// </summary>
- public static ConcurrentQueue<LogModel> Logs = new ConcurrentQueue<LogModel>();
- /// <summary>
- /// 发布一条日志记录
- /// </summary>
- /// <param name="path">路径</param>
- /// <param name="title">文件名</param>
- /// <param name="con">内容</param>
- public static void Publish(string path, string title, string con)
- {
- RedisHub.Default.RPush("LogHub", JsonConvert.SerializeObject(new LogModel
- {
- path = path,
- Title = title,
- Con = con
- }));
- }
- /// <summary>
- /// 增加一条处理日志
- /// </summary>
- /// <param name="title">文件名</param>
- /// <param name="con">内容</param>
- 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<LogModel>(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; }
- }
- }
|