终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MessageLog.cs 3.8 KiB

2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using BPASmartClient.Peripheral;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Collections.Concurrent;
  10. namespace BPASmartClient.Message
  11. {
  12. public class MessageLog
  13. {
  14. private volatile static MessageLog _Instance;
  15. public static MessageLog GetInstance => _Instance ?? (_Instance = new MessageLog());
  16. private MessageLog() { }
  17. #region 普通消息日志
  18. /// <summary>
  19. /// 日志显示委托
  20. /// </summary>
  21. public Action<string> InfoNotify { get; set; }
  22. /// <summary>
  23. /// 日志信息
  24. /// </summary>
  25. public string LogInfo { get; set; } = string.Empty;
  26. /// <summary>
  27. /// 普通日志输出
  28. /// </summary>
  29. /// <param name="info"></param>
  30. public void Show(string info)
  31. {
  32. Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}:{info}");
  33. LogInfo = $"{DateTime.Now.ToString("HH:mm:ss")}:{info} \n\r {LogInfo}";
  34. if (InfoNotify != null) InfoNotify(info);
  35. }
  36. #endregion
  37. #region 异常消息日志
  38. /// <summary>
  39. /// 异常日志委托
  40. /// </summary>
  41. public Action<string> ExInfoNotify { get; set; }
  42. /// <summary>
  43. /// 异常日志信息
  44. /// </summary>
  45. public string ExLogInfo { get; set; } = string.Empty;
  46. /// <summary>
  47. /// 异常日志输出
  48. /// </summary>
  49. /// <param name="info"></param>
  50. public void ShowEx(string info)
  51. {
  52. Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}:{info}");
  53. ExLogInfo = $"{DateTime.Now.ToString("HH:mm:ss")}:{info} \n\r {ExLogInfo}";
  54. if (ExInfoNotify != null) ExInfoNotify(info);
  55. }
  56. #endregion
  57. #region 设备过程日志
  58. /// <summary>
  59. /// 设备过程日志委托
  60. /// </summary>
  61. public Action<string> DeviceProcessLogNotify { get; set; }
  62. /// <summary>
  63. /// 设备日志信息字典
  64. /// </summary>
  65. public ConcurrentDictionary<string, string> DPLogInfo = new ConcurrentDictionary<string, string>();
  66. /// <summary>
  67. /// 设备日志输出
  68. /// </summary>
  69. /// <param name="info"></param>
  70. public void DeviceProcessLogShow(string id, string info)
  71. {
  72. if (!DPLogInfo.ContainsKey(id))
  73. {
  74. DPLogInfo.TryAdd(id, info);
  75. Debug.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}:{info}");
  76. //ExLogInfo = $"{DateTime.Now.ToString("HH:mm:ss")}:{info} \n\r {ExLogInfo}";
  77. if (DeviceProcessLogNotify != null) DeviceProcessLogNotify(info);
  78. }
  79. }
  80. #endregion
  81. #region 查找设备ID
  82. /// <summary>
  83. /// 查询设备ID
  84. /// </summary>
  85. /// <returns></returns>
  86. public object ReturnDeviceID()
  87. {
  88. object DeviceId = null;
  89. StackTrace trace = new StackTrace();
  90. for (int i = 1; i < 10; i++)
  91. {
  92. Type type = trace.GetFrame(i).GetMethod().ReflectedType;
  93. try
  94. {
  95. object obj = Activator.CreateInstance(type);
  96. if (obj is IPeripheral)
  97. {
  98. IPeripheral peripheral = obj as IPeripheral;
  99. DeviceId = type?.GetProperty("DeviceId")?.GetValue(peripheral, null);
  100. }
  101. if (DeviceId != null) break;
  102. }
  103. catch (Exception ex)
  104. {
  105. }
  106. }
  107. return DeviceId;
  108. }
  109. #endregion
  110. }
  111. }