电子药箱通讯服务端
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DebugHelper.cs 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace Rdh.SocketServer.Client
  10. {
  11. public sealed class DebugHelper
  12. {
  13. private static List<String> _distinctBuffer;
  14. private static Int32 _maxDistinctBufferLength;
  15. static DebugHelper()
  16. {
  17. if (Debugger.IsAttached)
  18. {
  19. _maxDistinctBufferLength = 5;
  20. _distinctBuffer = new List<string>();
  21. }
  22. }
  23. public static void Log(String message, [CallerMemberName] String memmber = null)
  24. {
  25. Debug.WriteLine(String.Format("{0} T:{1} {3} Log:{2}",
  26. DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),
  27. Thread.CurrentThread.ManagedThreadId.ToString(),
  28. message,
  29. memmber));
  30. }
  31. /// <summary>
  32. /// 使用去重缓存来记录日志,避免重复内容的记录
  33. /// </summary>
  34. /// <param name="message"></param>
  35. /// <param name="distinctDepth">去重深度,最大值为5,默认值为1</param>
  36. public static void LogDistinctly(String message, Int32 distinctDepth = 1)
  37. {
  38. if (Debugger.IsAttached)
  39. {
  40. if (_distinctBuffer.Count > 0)
  41. {
  42. if (distinctDepth > _distinctBuffer.Count)
  43. {
  44. distinctDepth = _distinctBuffer.Count;
  45. }
  46. for (Int32 i = 0; i < distinctDepth; i++)
  47. {
  48. if (_distinctBuffer[i] == message)
  49. {
  50. return;
  51. }
  52. }
  53. }
  54. Log(message);
  55. if (_distinctBuffer.Count >= _maxDistinctBufferLength)
  56. {
  57. _distinctBuffer.RemoveAt(_maxDistinctBufferLength - 1);
  58. }
  59. _distinctBuffer.Insert(0, message);
  60. }
  61. }
  62. public static void Log(Func<String> messageBuilder)
  63. {
  64. if (Debugger.IsAttached)
  65. {
  66. if (messageBuilder != null)
  67. {
  68. String message = messageBuilder();
  69. if (message != null)
  70. {
  71. Log(message);
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }