MainWorld.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using ServiceCenter.Logs;
  2. using System.Collections.Concurrent;
  3. using System.ComponentModel;
  4. using WCS.Core;
  5. using KnownException = ServiceCenter.Logs.KnownException;
  6. using LogInfo = ServiceCenter.Logs.LogInfo;
  7. namespace WCS.WorkEngineering.Worlds
  8. {
  9. /// <summary>
  10. /// 主世界,所有的系统(交互点)默认在该世界下执行。
  11. /// 如有系统需独立,请自行增加对应世界
  12. /// 新增世界应当继承此世界,而不是直接继承World
  13. /// </summary>
  14. [Description("主世界")]
  15. public class MainWorld : World
  16. {
  17. /// <summary>
  18. /// 构造函数
  19. /// </summary>
  20. public MainWorld()
  21. {
  22. }
  23. /// <summary>
  24. /// 日志队列
  25. /// </summary>
  26. protected ConcurrentQueue<KeyLog> Logs = new ConcurrentQueue<KeyLog>();
  27. /// <summary>
  28. /// 世界执行周期间隔
  29. /// 单位:毫秒
  30. /// </summary>
  31. protected override int Interval => 500;
  32. /// <summary>
  33. /// 更新前执行,重写改方法后请自行添加执行内容
  34. /// 执行内容:清空日志队列
  35. /// </summary>
  36. protected override void BeforeUpdate()
  37. {
  38. // 清空日志队列,确保日志队列中只会有当前周期日志
  39. Logs.Clear();
  40. }
  41. /// <summary>
  42. /// 更新后执行,重写改方法后请自行添加执行内容
  43. /// 执行内容:清空日志队列
  44. /// </summary>
  45. protected override void AfterUpdate()
  46. {
  47. LogHub.WorldPublish(Logs);
  48. }
  49. /// <summary>
  50. /// 异常处理,重写改方法后请自行添加执行内容
  51. /// 执行内容:Exception as KnownException并添加至日志队列
  52. /// </summary>
  53. /// <param name="channel"></param>
  54. /// <param name="exception"></param>
  55. /// <exception cref="NotImplementedException"></exception>
  56. protected override void OnError(Channel channel, Exception exception)
  57. {
  58. if (exception is KnownException)
  59. {
  60. var ex = exception as KnownException;
  61. var log = new LogInfo { Level = ex.Level, Type = ErrorTypeEnum.Kown, LogUpLoad = ex.logUpLoad, Message = ex.Message };
  62. Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now });
  63. }
  64. else
  65. {
  66. var log = new LogInfo { Level = LogLevelEnum.High, Type = ErrorTypeEnum.Unkown, LogUpLoad = LogUpLoadEnum.NotUpLoad, Message = exception.Message };
  67. Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now });
  68. }
  69. }
  70. /// <summary>
  71. /// 日志处理,重写改方法后请自行添加执行内容
  72. /// 执行内容:LogInfo as KeyLog并添加至日志队列
  73. /// </summary>
  74. /// <param name="channel"></param>
  75. /// <param name="logObj"></param>
  76. /// <exception cref="NotImplementedException"></exception>
  77. protected override void OnLog(Channel channel, object logObj)
  78. {
  79. var log = (LogInfo)logObj;
  80. Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now });
  81. }
  82. /// <summary>
  83. /// 日志处理,重写改方法后请自行添加执行内容
  84. /// </summary>
  85. /// <param name="channel"></param>
  86. /// <param name="msg"></param>
  87. /// <exception cref="NotImplementedException"></exception>
  88. protected override void OnInternalLog(Channel channel, string msg)
  89. {
  90. var log = new LogInfo { Level = LogLevelEnum.Low, Message = msg };
  91. Logs.Enqueue(new KeyLog { Channel = channel, Log = log, Time = DateTime.Now });
  92. }
  93. /// <summary>
  94. /// 获取日志,重写改方法后请自行添加执行内容
  95. /// </summary>
  96. /// <param name="channel"></param>
  97. /// <returns></returns>
  98. /// <exception cref="NotImplementedException"></exception>
  99. protected override IEnumerable<string> GetChannelMsg(Channel channel)
  100. {
  101. return Logs.Where(v => v.Channel.ToString() == channel.ToString()).Select(v => v.Log.ToString());
  102. }
  103. }
  104. }