123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using Newtonsoft.Json;
- using ServiceCenter.Extensions;
- using ServiceCenter.Redis;
- using System.Collections.Concurrent;
- namespace ServiceCenter.Logs
- {
- public class LogHub
- {
- /// <summary>
- /// 发布一条世界交互日志
- /// </summary>
- /// <param name="logs"></param>
- public static void WorldPublish(ConcurrentQueue<KeyLog> logs)
- {
- if (logs.Count > 0)
- {
- //每一条数据存入redis
- foreach (var log in logs)
- {
- var dir = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\{log.Channel.World}\\{log.Channel.System}\\{log.Channel.Item}\\";
- var msg = $"{log.Time.yyyyMMddhhmmssf()}--[{Thread.CurrentThread.ManagedThreadId}]--{log}\n";
- RedisHub.Default.RPush("LogHub", JsonConvert.SerializeObject(new LogModel
- {
- path = dir,
- Title = $"{log.Log.Message.Split(":")[0]}.txt",
- Con = msg
- }));
- }
- //存入业务报警内容
- List<BusinessAlarm> businesses = logs.Where(v => v.Log.Level != LogLevelEnum.Low).Select(v => new BusinessAlarm()
- {
- BusinessName = v.Channel.System,
- DevNo = v.Channel.Item,
- Con = v.ToString(),
- Time = DateTime.Now
- }).ToList();
- RedisHub.WMS.Set($"{nameof(BusinessAlarm)}", JsonConvert.SerializeObject(businesses));
- }
- }
- /// <summary>
- /// 发布一条日志记录
- /// </summary>
- /// <param name="title">接口名</param>
- /// <param name="con">内容</param>
- public static void InterfacePublish(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"
- }));
- }
- /// <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; }
- }
- /// <summary>
- /// 业务报警
- /// </summary>
- public class BusinessAlarm
- {
- /// <summary>
- /// 业务名称
- /// </summary>
- public string BusinessName { get; set; }
- /// <summary>
- /// 设备号
- /// </summary>
- public string DevNo { get; set; }
- /// <summary>
- /// 内容
- /// </summary>
- public string Con { get; set; }
- /// <summary>
- /// 时间
- /// </summary>
- public DateTime Time { get; set; }
- }
- }
|