LoggingExtensions.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Linq;
  3. using NModbus.Extensions;
  4. namespace NModbus.Logging
  5. {
  6. public static class LoggingExtensions
  7. {
  8. #region Standard level-based logging
  9. public static void Trace(this IModbusLogger logger, string message)
  10. {
  11. logger.Log(LoggingLevel.Trace, message);
  12. }
  13. public static void Debug(this IModbusLogger logger, string message)
  14. {
  15. logger.Log(LoggingLevel.Debug, message);
  16. }
  17. public static void Information(this IModbusLogger logger, string message)
  18. {
  19. logger.Log(LoggingLevel.Information, message);
  20. }
  21. public static void Warning(this IModbusLogger logger, string message)
  22. {
  23. logger.Log(LoggingLevel.Warning, message);
  24. }
  25. public static void Error(this IModbusLogger logger, string message)
  26. {
  27. logger.Log(LoggingLevel.Error, message);
  28. }
  29. public static void Critical(this IModbusLogger logger, string message)
  30. {
  31. logger.Log(LoggingLevel.Critical, message);
  32. }
  33. #endregion
  34. #region Func Logging
  35. public static void Log(this IModbusLogger logger, LoggingLevel level, Func<string> messageFactory)
  36. {
  37. if (logger.ShouldLog(level))
  38. {
  39. string message = messageFactory();
  40. logger.Log(level, message);
  41. }
  42. }
  43. public static void Trace(this IModbusLogger logger, Func<string> messageFactory)
  44. {
  45. logger.Log(LoggingLevel.Trace, messageFactory);
  46. }
  47. public static void Debug(this IModbusLogger logger, Func<string> messageFactory)
  48. {
  49. logger.Log(LoggingLevel.Debug, messageFactory);
  50. }
  51. public static void Information(this IModbusLogger logger, Func<string> messageFactory)
  52. {
  53. logger.Log(LoggingLevel.Information, messageFactory);
  54. }
  55. public static void Warning(this IModbusLogger logger, Func<string> messageFactory)
  56. {
  57. logger.Log(LoggingLevel.Warning, messageFactory);
  58. }
  59. public static void Error(this IModbusLogger logger, Func<string> messageFactory)
  60. {
  61. logger.Log(LoggingLevel.Error, messageFactory);
  62. }
  63. public static void Critical(this IModbusLogger logger, Func<string> messageFactory)
  64. {
  65. logger.Log(LoggingLevel.Critical, messageFactory);
  66. }
  67. #endregion
  68. #region Frame logging
  69. private static void LogFrame(this IModbusLogger logger, string validPrefix, string invalidPrefix, byte[] frame)
  70. {
  71. if (logger.ShouldLog(LoggingLevel.Trace))
  72. {
  73. if (logger.ShouldLog(LoggingLevel.Trace))
  74. {
  75. string prefix = frame.DoesCrcMatch() ? validPrefix : invalidPrefix;
  76. logger.Trace($"{prefix}: {string.Join(" ", frame.Select(b => b.ToString("X2")))}");
  77. }
  78. }
  79. }
  80. internal static void LogFrameTx(this IModbusLogger logger, byte[] frame)
  81. {
  82. logger.LogFrame("TX", "tx", frame);
  83. }
  84. internal static void LogFrameRx(this IModbusLogger logger, byte[] frame)
  85. {
  86. logger.LogFrame("RX", "rx", frame);
  87. }
  88. internal static void LogFrameIgnoreRx(this IModbusLogger logger, byte[] frame)
  89. {
  90. logger.LogFrame("IR", "ir", frame);
  91. }
  92. #endregion
  93. }
  94. }