| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 | using Newtonsoft.Json;using ServiceCenter.Extensions;using ServiceCenter.Redis;using System.Collections.Concurrent;using System.IO;using WCS.Core;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)        {            RedisHub.Default.RPush("LogHub", JsonConvert.SerializeObject(new LogModel            {                path = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\接口处理日志",                Title = $"{title}.txt",                Con = $"{DateTime.Now.yyyyMMddhhmmssf()}--[{Thread.CurrentThread.ManagedThreadId}]--{con}\n"            }));        }        public static void init()        {            while (true)            {                var logHub = RedisHub.Default.LPop("LogHub");                LogModel log = new LogModel();                try                {                    if (logHub != null)                    {                        log = JsonConvert.DeserializeObject<LogModel>(logHub);                        if (log != null)                        {                            if (!Directory.Exists(log.path)) Directory.CreateDirectory(log.path);                            File.AppendAllText(Path.Combine(log.path, log.Title), log.Con);                        }                    }                }                catch (Exception ex)                {                    var path = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\日志处理进程";                    if (!Directory.Exists(path)) Directory.CreateDirectory(path);                    File.AppendAllText(Path.Combine($"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\日志处理进程", "Error.txt"), $"{JsonConvert.SerializeObject(log)}--{ex.Message}--{ex.StackTrace}\n");                }            }        }    }    public class LogModel    {        public string path { get; set; }        public string Title { get; set; }        public string Con { get; set; }    }}
 |