/// <summary> /// 检测缓存是否存在 /// </summary> /// <param name="value">日志信息</param> /// <param name="isQueue">是否缓存队列</param> /// <returns>是否继续输出日志</returns> private bool CheckCache(DebugPlus value, bool isQueue) { hashString key = value.ToString(); if (isQueue) { interlocked.NoCheckCompareSetSleep0(ref _cacheLock); try { if (_cache.Get(key, false)) return false; _cache.Set(key, true); if (_cache.Count > _maxCacheCount) _cache.Pop(); } finally { _cacheLock = 0; } return true; } if (key.Equals(_lastCache)) return false; _lastCache = key; return true; }
/// <summary> /// 日志信息写文件 /// </summary> /// <param name="value">日志信息</param> private void RealOutput(DebugPlus value) { if (_isDisposed == 0) { if (_fileStream == null) { Console.WriteLine(@" " + date.NowSecond.toString() + " : " + value.ToString()); } else { memoryPool.pushSubArray data = _fileStream.GetBytes(@" " + date.NowSecond.toString() + " : " + value.ToString() + @" "); if (Interlocked.CompareExchange(ref _fileLock, 1, 0) != 0) { Thread.Sleep(0); while (Interlocked.CompareExchange(ref _fileLock, 1, 0) != 0) Thread.Sleep(1); } try { if (_fileStream.UnsafeWrite(data) >= MaxSize && MaxSize > 0) MoveBakBase(); else _fileStream.Flush(true); } finally { _fileLock = 0; } } } }
/// <summary> /// 实时添加日志并抛出异常 /// </summary> /// <param name="error">异常类型</param> public void ThrowReal(ExceptionTypeEnum error) { var value = new DebugPlus { StackTrace = new StackTrace(), Type = error }; if (CheckCache(value, true)) RealOutput(value); throw new Exception(ExceptionPrefix + value.ToString()); }