LogHelper.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using Library;
  6. using LitJson;
  7. using MySystem;
  8. public class LogHelper
  9. {
  10. public readonly static LogHelper Instance = new LogHelper();
  11. private LogHelper()
  12. { }
  13. public void WriteLog(string Content, string Topic)
  14. {
  15. Dictionary<string, string> dic = new Dictionary<string, string>();
  16. dic.Add("Topic", Topic);
  17. dic.Add("Content", Content);
  18. RedisDbconn.Instance.AddList("SlsLogQueue", Newtonsoft.Json.JsonConvert.SerializeObject(dic));
  19. }
  20. public void Start()
  21. {
  22. Thread th = new Thread(DoWorks);
  23. th.IsBackground = true;
  24. th.Start();
  25. }
  26. public void DoWorks()
  27. {
  28. while (true)
  29. {
  30. string content = RedisDbconn.Instance.RPop<string>("SlsLogQueue");
  31. if (!string.IsNullOrEmpty(content))
  32. {
  33. try
  34. {
  35. DoQueue(content);
  36. }
  37. catch (Exception ex)
  38. {
  39. Utils.WriteLog(DateTime.Now.ToString() + "\n" + content + "\n" + ex, "SLS日志异常");
  40. }
  41. }
  42. else
  43. {
  44. Thread.Sleep(5000);
  45. }
  46. }
  47. }
  48. public void DoQueue(string content)
  49. {
  50. JsonData JsonObj = JsonMapper.ToObject(content);
  51. string Topic = JsonObj["Topic"].ToString();
  52. string Cont = JsonObj["Content"].ToString();
  53. // string Tags = JsonObj["Tags"].ToString();
  54. Dictionary<string, string> otherTags = new Dictionary<string, string>();
  55. // if(!string.IsNullOrEmpty(Tags))
  56. // {
  57. // string[] TagList = Tags.Split(',');
  58. // JsonData contObj = JsonMapper.ToObject(Cont);
  59. // foreach(string Tag in TagList)
  60. // {
  61. // otherTags.Add(key: Tag, contObj[Tag].ToString());
  62. // }
  63. // }
  64. SLS.WriteLog(DateTime.Now, Topic, Cont, otherTags);
  65. }
  66. }