/// <summary> /// 日志信息写文件 /// </summary> /// <param name="value">日志信息</param> private void realOutput(debug value) { if (isDisposed == 0) { if (isFieStream) { memoryPool.pushSubArray data = fileStreamWriter.GetBytes(@" " + date.nowTime.Now.toString() + " : " + value.ToString() + @" ", fastCSharp.config.appSetting.Encoding); Monitor.Enter(fileLock); try { if (fileStream != null) { if (fileStream.UnsafeWrite(ref data) >= MaxSize && MaxSize > 0) { moveBak(); } else { fileStream.Flush(true); } return; } } finally { Monitor.Exit(fileLock); } } Console.WriteLine(@" " + date.nowTime.Now.toString() + " : " + value.ToString()); } }
/// <summary> /// 实时添加日志并抛出异常 /// </summary> /// <param name="error">异常类型</param> public void ThrowReal(exceptionType error) { debug value = new debug { StackTrace = new StackTrace(), Type = error }; if (!checkCache(value)) { output(value.ToString()); } throw new Exception(ExceptionPrefix + value.ToString()); }
/// <summary> /// 实时添加日志并抛出异常 /// </summary> /// <param name="error">错误异常</param> /// <param name="message">提示信息</param> /// <param name="isCache">是否缓存</param> public void ThrowReal(Exception error, string message = null, bool isCache = true) { if (error != null && error.Message.StartsWith(ExceptionPrefix, StringComparison.Ordinal)) { error = null; } if (error == null) { if (message != null) { ThrowReal(message, true, isCache); } } else { debug value = new debug { Exception = error, Message = message }; if (!isCache || !checkCache(value)) { output(value.ToString()); } throw error != null ? new Exception(ExceptionPrefix + message, error) : new Exception(ExceptionPrefix + message); } }
/// <summary> /// 检测缓存是否存在 /// </summary> /// <param name="value">日志信息</param> /// <param name="isQueue">是否缓存队列</param> /// <returns>是否继续输出日志</returns> private bool checkCache(debug value, bool isQueue) { hashString key = value.ToString(); if (isQueue) { Monitor.Enter(cacheLock); try { if (cache.Get(key, false)) { return(false); } cache.Set(key, true); if (cache.Count > maxCacheCount) { cache.UnsafePopValue(); } } finally { Monitor.Exit(cacheLock); } return(true); } if (key.Equals(lastCache)) { return(false); } lastCache = key; return(true); }
/// <summary> /// 实时添加日志 /// </summary> /// <param name="message">提示信息</param> /// <param name="isStackTrace">是否包含调用堆栈</param> /// <param name="isCache">是否缓存</param> public void Real(string message, bool isStackTrace = false, bool isCache = false) { debug value = new debug { StackTrace = isStackTrace ? new StackTrace() : null, StackFrame = isStackTrace ? null : new StackFrame(1), Message = message }; if (!isCache || !checkCache(value)) { output(value.ToString()); } }
/// <summary> /// 添加日志并抛出异常 /// </summary> /// <param name="error">异常类型</param> public void Throw(exceptionType error) { debug value = new debug { StackTrace = new StackTrace(true), Type = error }; if (checkCache(value, true)) { queue.EnqueueNotNull(value); } throw new Exception(ExceptionPrefix + value.ToString()); }
/// <summary> /// 检测缓存是否存在 /// </summary> /// <param name="value">日志信息</param> /// <returns>缓存是否存在</returns> private bool checkCache(debug value) { string key = value.ToString(); while (Interlocked.CompareExchange(ref cacheLock, 1, 0) != 0) { Thread.Sleep(1); } try { if (cache.ContainsKey(key)) { return(true); } cache.Add(key, value); } finally { cacheLock = 0; } return(false); }
/// <summary> /// 实时添加日志并抛出异常 /// </summary> /// <param name="message">提示信息</param> /// <param name="isStackTrace">是否包含调用堆栈</param> /// <param name="isCache">是否缓存</param> public void ThrowReal(string message, bool isStackTrace = false, bool isCache = false) { debug value = new debug { StackTrace = isStackTrace ? new StackTrace() : null, StackFrame = isStackTrace ? null : new StackFrame(1), Message = message }; if (!isCache || !checkCache(value)) output(value.ToString()); throw new Exception(ExceptionPrefix + message); }
/// <summary> /// 实时添加日志并抛出异常 /// </summary> /// <param name="error">错误异常</param> /// <param name="message">提示信息</param> /// <param name="isCache">是否缓存</param> public void ThrowReal(Exception error, string message = null, bool isCache = true) { if (error != null && error.Message.StartsWith(ExceptionPrefix, StringComparison.Ordinal)) error = null; if (error == null) { if (message != null) ThrowReal(message, true, isCache); } else { debug value = new debug { Exception = error, Message = message }; if (!isCache || !checkCache(value)) output(value.ToString()); throw error != null ? new Exception(ExceptionPrefix + message, error) : new Exception(ExceptionPrefix + message); } }
/// <summary> /// 实时添加日志并抛出异常 /// </summary> /// <param name="error">异常类型</param> public void ThrowReal(exceptionType error) { debug value = new debug { StackTrace = new StackTrace(), Type = error }; if (!checkCache(value)) output(value.ToString()); throw new Exception(ExceptionPrefix + value.ToString()); }
/// <summary> /// 检测缓存是否存在 /// </summary> /// <param name="value">日志信息</param> /// <returns>缓存是否存在</returns> private bool checkCache(debug value) { string key = value.ToString(); while (Interlocked.CompareExchange(ref cacheLock, 1, 0) != 0) Thread.Sleep(1); try { if (cache.ContainsKey(key)) return true; cache.Add(key, value); } finally { cacheLock = 0; } return false; }