LogHelper.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 FileName, string BrandId = "0")
  14. {
  15. function.WriteLog(Content, FileName);
  16. // Dictionary<string, string> dic = new Dictionary<string, string>();
  17. // dic.Add("Topic", FileName);
  18. // dic.Add("Content", Content);
  19. // dic.Add("BrandId", BrandId);
  20. // RedisDbconn.Instance.AddList("LogQueue", Newtonsoft.Json.JsonConvert.SerializeObject(dic));
  21. }
  22. public void Start()
  23. {
  24. Thread th = new Thread(DoWorks);
  25. th.IsBackground = true;
  26. th.Start();
  27. }
  28. public void DoWorks()
  29. {
  30. while (true)
  31. {
  32. string content = RedisDbconn.Instance.RPop<string>("LogQueue");
  33. if (!string.IsNullOrEmpty(content))
  34. {
  35. try
  36. {
  37. DoQueue(content);
  38. }
  39. catch (Exception ex)
  40. {
  41. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + content + "\n" + ex, "SLS日志异常");
  42. }
  43. }
  44. else
  45. {
  46. Thread.Sleep(5000);
  47. }
  48. }
  49. }
  50. public void DoQueue(string content)
  51. {
  52. JsonData JsonObj = JsonMapper.ToObject(content);
  53. string Topic = JsonObj["Topic"].ToString();
  54. string Cont = JsonObj["Content"].ToString();
  55. string BrandId = JsonObj["BrandId"].ToString();
  56. // SLS.WriteLog(DateTime.Now, Topic, Cont, new Dictionary<string, string>()
  57. // {
  58. // {"BrandId", BrandId}
  59. // });
  60. }
  61. }