LogService.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using static IoTIntegrationPlatform.Model.Enum.EnumMqtt;
  7. using IoTIntegrationPlatform.Common;
  8. using IoTIntegrationPlatform.Interface;
  9. using IoTIntegrationPlatform.Model.common;
  10. using IoTIntegrationPlatform.Model.Model;
  11. using SqlSugar;
  12. namespace IoTIntegrationPlatform.Services
  13. {
  14. /// <summary>
  15. /// 日志服务
  16. /// </summary>
  17. public class LogService : ILogService
  18. {
  19. private ISqlSugarClient db { get; }
  20. public LogService(ISqlSugarClient _db)
  21. {
  22. db = _db;
  23. }
  24. #region 类句柄操作
  25. private IntPtr handle;
  26. private bool disposed = false;
  27. //关闭句柄
  28. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1060:MovePInvokesToNativeMethodsClass"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), System.Runtime.InteropServices.DllImport("Kernel32")]
  29. public extern static Boolean CloseHandle(IntPtr handle);
  30. /// <summary>
  31. /// 释放对象资源
  32. /// </summary>
  33. public void Dispose()
  34. {
  35. Dispose(true);
  36. GC.SuppressFinalize(this);
  37. }
  38. /// <summary>
  39. /// 释放对象资源
  40. /// </summary>
  41. /// <param name="disposing"></param>
  42. protected virtual void Dispose(bool disposing)
  43. {
  44. if (!this.disposed)
  45. {
  46. if (handle != IntPtr.Zero)
  47. {
  48. CloseHandle(handle);
  49. handle = IntPtr.Zero;
  50. }
  51. }
  52. disposed = true;
  53. }
  54. ~LogService()
  55. {
  56. Dispose(false);
  57. }
  58. #endregion
  59. /// <summary>
  60. /// 获取mqtt通讯日志列表
  61. /// </summary>
  62. /// <param name="parm"></param>
  63. /// <param name="totalPage"></param>
  64. /// <returns></returns>
  65. public List<MqttLog> GetMqttLogList(BaseParm parm, ref int totalPage)
  66. {
  67. List<MqttLog> list = new List<MqttLog>();
  68. try
  69. {
  70. list = db.Queryable<MqttLog>()
  71. .WhereIF(!string.IsNullOrEmpty(parm.Param), it => it.ClientId.Contains(parm.Param) || it.Content.Contains(parm.Param))
  72. .WhereIF(parm.Int1 > 0, it => it.MessagePattern == parm.Int1)
  73. .WhereIF(parm.Int2 > 0, it => it.TopicType == (EnumMqttTopicType)parm.Int2)
  74. .OrderBy(it => it.CreateTime, OrderByType.Desc)//倒序
  75. .ToPageList(parm.PageIndex, parm.PageSize, ref totalPage);
  76. }
  77. catch (Exception ex)
  78. {
  79. Logging.Error(ex, "GetMqttLogList");
  80. }
  81. return list;
  82. }
  83. /// <summary>
  84. /// 添加mqtt通讯日志
  85. /// </summary>
  86. /// <param name="level"></param>
  87. /// <param name="content"></param>
  88. /// <returns></returns>
  89. public bool AddMqttLog(MqttLog model)
  90. {
  91. bool result = false;
  92. try
  93. {
  94. model.Uuid = Guid.NewGuid().ToString();
  95. model.TopicTypeName = EnumExtension.GetDescription(model.TopicType);
  96. model.CreateTime = TimeHelper.GetCurrentTimestampTicks();
  97. int row = db.Insertable(model).ExecuteCommand();
  98. if (row > 0)
  99. {
  100. result = true;
  101. }
  102. }
  103. catch (Exception ex)
  104. {
  105. Logging.Error(ex, ex.Message);
  106. }
  107. return result;
  108. }
  109. }
  110. }