123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
-
- namespace Rdh.SocketServer.Client
- {
- public sealed class DebugHelper
- {
- private static List<String> _distinctBuffer;
- private static Int32 _maxDistinctBufferLength;
- static DebugHelper()
- {
- if (Debugger.IsAttached)
- {
- _maxDistinctBufferLength = 5;
- _distinctBuffer = new List<string>();
- }
- }
- public static void Log(String message, [CallerMemberName] String memmber = null)
- {
- Debug.WriteLine(String.Format("{0} T:{1} {3} Log:{2}",
- DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"),
- Thread.CurrentThread.ManagedThreadId.ToString(),
- message,
- memmber));
- }
- /// <summary>
- /// 使用去重缓存来记录日志,避免重复内容的记录
- /// </summary>
- /// <param name="message"></param>
- /// <param name="distinctDepth">去重深度,最大值为5,默认值为1</param>
- public static void LogDistinctly(String message, Int32 distinctDepth = 1)
- {
- if (Debugger.IsAttached)
- {
- if (_distinctBuffer.Count > 0)
- {
- if (distinctDepth > _distinctBuffer.Count)
- {
- distinctDepth = _distinctBuffer.Count;
- }
- for (Int32 i = 0; i < distinctDepth; i++)
- {
- if (_distinctBuffer[i] == message)
- {
- return;
- }
- }
- }
- Log(message);
- if (_distinctBuffer.Count >= _maxDistinctBufferLength)
- {
- _distinctBuffer.RemoveAt(_maxDistinctBufferLength - 1);
- }
- _distinctBuffer.Insert(0, message);
- }
- }
- public static void Log(Func<String> messageBuilder)
- {
- if (Debugger.IsAttached)
- {
- if (messageBuilder != null)
- {
- String message = messageBuilder();
- if (message != null)
- {
- Log(message);
- }
- }
- }
- }
- }
- }
|