123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- using Newtonsoft.Json;
- using ServiceCenter.Extensions;
- using ServiceCenter.Redis;
- using System.Collections.Concurrent;
- using System.Diagnostics;
- using WCS.Core;
- namespace ServiceCenter.Logs
- {
- public static class LogHub
- {
- /// <summary>
- /// 发布世界交互日志
- /// </summary>
- /// <param name="logs"></param>
- /// <param name="World">当前世界</param>
- public static void WorldPublish(ConcurrentQueue<KeyLog> logs, string World)
- {
- if (logs.Count > 0)
- {
- //var sql = new StringBuilder();
- //sql.Append("INSERT INTO ");
- //var db = new SqlSugarHelper().PLC;
- try
- {
- Parallel.ForEach(logs.GroupBy(x => x.Channel.System), item1 =>
- {
- Parallel.ForEach(item1.GroupBy(x => x.Channel.Item), item2 =>
- {
- var msgList = item2.Select(x => x.Log.Message).ToList();
- RedisHub.Default.Set($"WCSLog:{item1.Key}:{item2.Key}", JsonConvert.SerializeObject(msgList));
- });
- });
- //每一条数据存入redis
- foreach (var log in logs)
- {
- try
- {
- 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";
- var title = $"{log.Log.Message.Split(":")[0]}.txt";
- RedisHub.Default.RPush("LogHub", JsonConvert.SerializeObject(new LogModel
- {
- path = dir,
- Title = title,
- Con = msg
- }));
- //sql.Append(GetString(db.Insertable(new WCS_Log()
- //{
- // World = log.Channel.World,
- // System = log.Channel.System,
- // Code = log.Channel.Item.ToLower().Replace(".", ""),
- // Title = log.Log.Message.Split(":")[0],
- // ExTime = log.Time,
- // ManagedThreadId = Thread.CurrentThread.ManagedThreadId,
- // Content = log.Log.Message.RemoveEscapeCharacters().Substring(0, 254)
- //}).ToSqlString()));
- }
- catch (Exception e)
- {
- }
- }
- //var sqlText = sql.ToString();
- //if (sqlText.Contains("russ1"))
- //{
- // var a = 1;
- //}
- //db.Ado.ExecuteCommand(sql.ToString());
- //存入业务报警内容
- List<BusinessAlarm> businesses = logs.Where(v => v.Log.Level > LogLevelEnum.Low && v.Log.LogUpLoad == LogUpLoadEnum.UpLoadWMS).Select(v => new BusinessAlarm()
- {
- BusinessName = v.Channel.System,
- DevNo = v.Channel.Item,
- Con = v.ToString(),
- Time = DateTime.Now
- }).ToList();
- RedisHub.WMS.Set($"{nameof(BusinessAlarm)}:{World}", JsonConvert.SerializeObject(businesses));
- }
- catch
- {
- }
- }
- }
- /// <summary>
- /// 去除转义字符
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- public static string RemoveEscapeCharacters(this string? value)
- {
- return value.Trim('\0', '\a', '\b', '\f', '\n', '\r', '\t', '\v').Trim();
- }
- public static string GetString(string value)
- {
- return value.Replace("INSERT INTO ", "")
- .Replace(",N'", ",'")
- .Replace("\0", "")
- .Replace("wcs_", "")
- .Replace("(N'", "('") + "\r";
- }
- /// <summary>
- /// 执行记录
- /// </summary>
- /// <param name="system">系统</param>
- /// <param name="devCode">设备号</param>
- /// <param name="msg">内容</param>
- public static void ExRecord(this SystemBase system, string devCode, string msg)
- {
- //var key = $"{system.World.Description}:{devCode}";
- //RedisHub.Default.RPush($"{system.World.Description}:{devCode}", msg);
- //if (RedisHub.Monitor.LLen(key) > 5000)
- //{
- // RedisHub.Monitor.LTrim(key, 4000, -1);
- //}
- }
- /// <summary>
- /// 执行记录
- /// </summary>
- /// <param name="system">系统</param>
- /// <param name="devCode">设备号</param>
- /// <param name="msg">内容</param>
- public static async void ExRecord<T>(this SystemBase system, string devCode, string msg, List<T> ints)
- {
- try
- {
- //var key = $"{system.World.Description}:{devCode}";
- //RedisHub.Default.RPush(key, msg + ints.JsonToString());
- //if (RedisHub.Monitor.LLen(key) > 5000)
- //{
- // RedisHub.Monitor.LTrim(key, 4000, -1);
- //}
- }
- catch (Exception e)
- {
- //Console.WriteLine(e);
- }
- }
- /// <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()}--{con}\n"
- }));
- }
- /// <summary>
- /// 初始化日志处理进程
- /// </summary>
- public static void init()
- {
- while (true)
- {
- var sw = new Stopwatch();
- sw.Start();
- LogModel log = new LogModel();
- try
- {
- //var res = RedisHub.Default.LRange("LogHub", 0, 5000).ToList();
- //var logHubList = new List<LogModel>();
- //Parallel.ForEach(res, x =>
- //{
- // var log = JsonConvert.DeserializeObject<LogModel>(x);
- // if (log != null)
- // {
- // logHubList.Add(log);
- // }
- //});
- //Parallel.ForEach(logHubList.GroupBy(x => x.path).ToList(), logHub =>
- //{
- // Parallel.ForEach(logHub.GroupBy(x => x.Title).ToList(), log =>
- // {
- // foreach (var item in log)
- // {
- // if (item.Con.IsNullOrEmpty())
- // {
- // if (!Directory.Exists(item.path)) Directory.CreateDirectory(item.path);
- // File.AppendAllText(Path.Combine(item.path, item.Title), item.Con);
- // }
- // }
- // });
- //});
- //RedisHub.Default.LRem("LogHub", 0, 5000);
- //foreach (var logHub in logHubList)
- //{
- // try
- // {
- // log = ;
- // if (log != null)
- // {
- // if (!Directory.Exists(log.path)) Directory.CreateDirectory(log.path);
- // File.AppendAllText(Path.Combine(log.path, log.Title), log.Con);
- // count++;
- // }
- // }
- // catch (Exception e)
- // {
- // // ignored
- // }
- //}
- var logHub = RedisHub.Default.BLPop("LogHub", 0);
- log = JsonConvert.DeserializeObject<LogModel>(logHub);
- if (log != null)
- {
- if (!Directory.Exists(log.path)) Directory.CreateDirectory(log.path);
- //FileInfo fileInfo = new FileInfo(Path.Combine(log.path, log.Title));
- //if (fileInfo.Length > 5000)
- //{
- // var title = log.Title.Split(".");
- // File.Move(log.Title, title[0] + "" + title[1]);
- //}
- 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");
- }
- sw.Stop();
- var path1 = $"D:\\WCSLogs\\{DateTime.Now.yyyyMMdd()}\\日志处理进程";
- if (!Directory.Exists(path1)) Directory.CreateDirectory(path1);
- File.AppendAllText(Path.Combine(path1, "耗时.txt"), $"耗时:{sw.ElapsedMilliseconds}\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; }
- }
- }
|