using Newtonsoft.Json; using ServiceCenter.Extensions; using ServiceCenter.Redis; using ServiceCenter.SqlSugars; using System.Collections.Concurrent; using System.Diagnostics; using System.Text; using WCS.Core; using WCS.Entity.Protocol.RGV; namespace ServiceCenter.Logs { public static class LogHub { /// /// 发布世界交互日志 /// /// /// 当前世界 public static void WorldPublish(ConcurrentQueue logs, string World) { if (logs.Count > 0) { //var sql = new StringBuilder(); //sql.Append("INSERT INTO "); //var db = new SqlSugarHelper().PLC; try { //每一条数据存入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 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 { } } } /// /// 去除转义字符 /// /// /// 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"; } /// /// 执行记录 /// /// 系统 /// 设备号 /// 内容 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); //} } /// /// 执行记录 /// /// 系统 /// 设备号 /// 内容 public static async void ExRecord(this SystemBase system, string devCode, string msg, List 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); } } /// /// 发布一条日志记录 /// /// 接口名 /// 内容 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" })); } /// /// 初始化日志处理进程 /// 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(); //Parallel.ForEach(res, x => //{ // var log = JsonConvert.DeserializeObject(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(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; } } /// /// 业务报警 /// public class BusinessAlarm { /// /// 业务名称 /// public string BusinessName { get; set; } /// /// 设备号 /// public string DevNo { get; set; } /// /// 内容 /// public string Con { get; set; } /// /// 时间 /// public DateTime Time { get; set; } } }