| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- 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<string, string> dic = new Dictionary<string, string>();
- 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<string>("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<string, string> otherTags = new Dictionary<string, string>();
- 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);
- }
- }
|