using System; using System.Collections.Generic; using System.IO; using System.Threading; using Library; using LitJson; using MySystem; public class LogHelper { public readonly static LogHelper Instance = new LogHelper(); private LogHelper() { } public void WriteLog(string Content, string Topic) { Dictionary dic = new Dictionary(); dic.Add("Topic", Topic); dic.Add("Content", Content); RedisDbconn.Instance.AddList("SlsLogQueue", Newtonsoft.Json.JsonConvert.SerializeObject(dic)); } public void Start() { Thread th = new Thread(DoWorks); th.IsBackground = true; th.Start(); } public void DoWorks() { while (true) { string content = RedisDbconn.Instance.RPop("SlsLogQueue"); if (!string.IsNullOrEmpty(content)) { try { DoQueue(content); } catch (Exception ex) { LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + content + "\n" + ex, "SLS日志异常"); } } else { Thread.Sleep(5000); } } } public void DoQueue(string content) { JsonData JsonObj = JsonMapper.ToObject(content); string Topic = JsonObj["Topic"].ToString(); string Cont = JsonObj["Content"].ToString(); string Tags = JsonObj["Tags"].ToString(); Dictionary otherTags = new Dictionary(); if(!string.IsNullOrEmpty(Tags)) { string[] TagList = Tags.Split(','); JsonData contObj = JsonMapper.ToObject(Cont); foreach(string Tag in TagList) { otherTags.Add(key: Tag, contObj[Tag].ToString()); } } SLS.WriteLog(DateTime.Now, Topic, Cont, otherTags); } }