using Newtonsoft.Json;
using ServiceCenter.Extensions;
using ServiceCenter.Redis;
using System.Collections.Concurrent;
using WCS.Core;
namespace ServiceCenter.Logs
{
public static class LogHub
{
///
/// 发布世界交互日志
///
///
/// 当前世界
public static void WorldPublish(ConcurrentQueue logs, string World)
{
if (logs.Count > 0)
{
try
{
//每一条数据存入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 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 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)
{
LogModel log = new LogModel();
try
{
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");
}
}
}
}
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; }
}
}