/// <summary>输出日志</summary> /// <param name="msg">信息</param> public void WriteLine(String msg) { // 小对象,采用对象池的成本太高了 var e = new WriteLogEventArgs(msg, null, true); PerformWriteLog(e); }
private void ConsoleWriteLog(WriteLogEventArgs e) { if (LastIsNewLine) { var msg = e.ToString(); // 如果上一次是换行,则这次需要输出行头信息 if (e.IsNewLine) { Console.WriteLine(msg); } else { Console.Write(msg); LastIsNewLine = false; } } else { // 如果上一次不是换行,则这次不需要行头信息 var msg = e.Message + e.Exception; if (e.IsNewLine) { Console.WriteLine(msg); LastIsNewLine = true; } else { Console.Write(msg); } } }
/// <summary>输出日志</summary> /// <param name="msg">信息</param> public static void WriteLine(String msg) { InitLog(); if (OnWriteLog != null) { var e = new WriteLogEventArgs(msg, null, true); OnWriteLog(null, e); } if (UseFileLog) Log.WriteLine(msg); }
/// <summary>使用线程池线程异步执行日志写入动作</summary> /// <param name="e"></param> protected virtual void PerformWriteLog(WriteLogEventArgs e) { lock (Log_Lock) { try { // 初始化日志读写器 if (LogWriter == null) { InitLog(); } // 写日志 if (LastIsNewLine) { // 如果上一次是换行,则这次需要输出行头信息 if (e.IsNewLine) { LogWriter.WriteLine(e.ToString()); } else { LogWriter.Write(e.ToString()); LastIsNewLine = false; } } else { // 如果上一次不是换行,则这次不需要行头信息 var msg = e.Message + e.Exception; if (e.IsNewLine) { LogWriter.WriteLine(msg); LastIsNewLine = true; } else { LogWriter.Write(msg); } } // 声明自动关闭日志读写器的定时器。无限延长时间,实际上不工作 if (AutoCloseWriterTimer == null) { AutoCloseWriterTimer = new Timer(new TimerCallback(CloseWriter), null, Timeout.Infinite, Timeout.Infinite); } // 改变定时器为5秒后触发一次。如果5秒内有多次写日志操作,估计定时器不会触发,直到空闲五秒为止 AutoCloseWriterTimer.Change(5000, Timeout.Infinite); // 清空日志对象 e.Clear(); } catch { } } }
/// <summary>写日志</summary> /// <param name="format"></param> /// <param name="args"></param> public static void Write(String format, params Object[] args) { InitLog(); if (OnWriteLog != null) { var msg = String.Format(format, args); var e = new WriteLogEventArgs(msg, null, false); OnWriteLog(null, e); } if (UseFileLog) Log.Write(format, args); }
/// <summary>输出异常日志</summary> /// <param name="ex">异常信息</param> public static void WriteException(Exception ex) { InitLog(); if (OnWriteLog != null) { var e = new WriteLogEventArgs(null, ex, true); OnWriteLog(null, e); } if (UseFileLog) { Log.WriteException(ex); } }
/// <summary>输出日志</summary> /// <param name="msg">信息</param> public static void WriteLine(String msg) { InitLog(); if (OnWriteLog != null) { var e = new WriteLogEventArgs(msg, null, true); OnWriteLog(null, e); } if (UseFileLog) { Log.WriteLine(msg); } }
/// <summary>写日志</summary> /// <param name="format"></param> /// <param name="args"></param> public static void WriteLine(String format, params Object[] args) { InitLog(); if (OnWriteLog != null) { var msg = String.Format(format, args); var e = new WriteLogEventArgs(msg, null, true); OnWriteLog(null, e); } if (UseFileLog) { Log.WriteLine(format, args); } }
private static void XTrace_OnWriteLog2(object sender, WriteLogEventArgs e) { lock (dic) { ConsoleColor cc; var key = e.ThreadID; if (!dic.TryGetValue(key, out cc)) { //lock (dic) { //if (!dic.TryGetValue(key, out cc)) { cc = colors[dic.Count % 7]; dic[key] = cc; } } } var old = Console.ForegroundColor; Console.ForegroundColor = cc; ConsoleWriteLog(e); Console.ForegroundColor = old; } }
/// <summary>使用线程池线程异步执行日志写入动作</summary> /// <param name="e"></param> protected virtual void PerformWriteLog(WriteLogEventArgs e) { lock (Log_Lock) { try { // 初始化日志读写器 if (LogWriter == null) { InitLog(); } // 写日志 LogWriter.WriteLine(e.ToString()); // 声明自动关闭日志读写器的定时器。无限延长时间,实际上不工作 if (_Timer == null) { _Timer = new Timer(CloseWriter, null, Timeout.Infinite, Timeout.Infinite); } // 改变定时器为5秒后触发一次。如果5秒内有多次写日志操作,估计定时器不会触发,直到空闲五秒为止 _Timer.Change(5000, Timeout.Infinite); } catch { } } }
private void ConsoleWriteLog(WriteLogEventArgs e) { var msg = e.ToString(); Console.WriteLine(msg); }
/// <summary>输出日志</summary> /// <param name="msg">信息</param> public void Write(String msg) { var e = new WriteLogEventArgs(msg, null, false); PerformWriteLog(e); }
/// <summary>使用线程池线程异步执行日志写入动作</summary> /// <param name="e"></param> private void PerformWriteLog(WriteLogEventArgs e) { lock (Log_Lock) { try { // 初始化日志读写器 if (LogWriter == null) InitLog(); // 写日志 if (LastIsNewLine) { // 如果上一次是换行,则这次需要输出行头信息 if (e.IsNewLine) LogWriter.WriteLine(e.ToString()); else { LogWriter.Write(e.ToString()); LastIsNewLine = false; } } else { // 如果上一次不是换行,则这次不需要行头信息 var msg = e.Message + e.Exception; if (e.IsNewLine) { LogWriter.WriteLine(msg); LastIsNewLine = true; } else LogWriter.Write(msg); } // 声明自动关闭日志读写器的定时器。无限延长时间,实际上不工作 if (AutoCloseWriterTimer == null) AutoCloseWriterTimer = new Timer(new TimerCallback(CloseWriter), null, Timeout.Infinite, Timeout.Infinite); // 改变定时器为5秒后触发一次。如果5秒内有多次写日志操作,估计定时器不会触发,直到空闲五秒为止 AutoCloseWriterTimer.Change(5000, Timeout.Infinite); } catch { } } }
/// <summary>输出异常日志</summary> /// <param name="ex">异常信息</param> public static void WriteException(Exception ex) { InitLog(); if (OnWriteLog != null) { var e = new WriteLogEventArgs(null, ex, true); OnWriteLog(null, e); } if (UseFileLog) Log.WriteException(ex); }
private static void XTrace_OnWriteLog(object sender, WriteLogEventArgs e) { //Console.WriteLine(e.ToString()); ConsoleWriteLog(e); }
private static void ConsoleWriteLog(WriteLogEventArgs e) { if (LastIsNewLine) { // 如果上一次是换行,则这次需要输出行头信息 if (e.IsNewLine) Console.WriteLine(e.ToString()); else { Console.Write(e.ToString()); LastIsNewLine = false; } } else { // 如果上一次不是换行,则这次不需要行头信息 var msg = e.Message + e.Exception; if (e.IsNewLine) { Console.WriteLine(msg); LastIsNewLine = true; } else Console.Write(msg); } }
/// <summary>输出异常日志</summary> /// <param name="ex">异常信息</param> public void WriteException(Exception ex) { var e = new WriteLogEventArgs(null, ex, true); PerformWriteLog(e); }
/// <summary> /// 输出日志 /// </summary> /// <param name="msg">信息</param> public void WriteLine(String msg) { WriteLogEventArgs e = new WriteLogEventArgs(msg); if (OnWriteLog != null) { OnWriteLog(null, e); return; } PerformWriteLog(e.ToString()); }
/// <summary>输出日志</summary> /// <param name="msg">信息</param> public void WriteLine(String msg) { // 小对象,采用对象池的成本太高了 var e = new WriteLogEventArgs(msg); //// 采用对象池,避免太多小对象造成GC压力 //var e = WriteLogEventArgs.Create(msg, null); //try //{ if (OnWriteLog != null) { OnWriteLog(null, e); return; } PerformWriteLog(e.ToString()); //} //finally { WriteLogEventArgs.Push(e); } }
/// <summary>输出异常日志</summary> /// <param name="ex">异常信息</param> public void WriteException(Exception ex) { var e = new WriteLogEventArgs(null, ex); if (OnWriteLog != null) { OnWriteLog(null, e); return; } PerformWriteLog(e.ToString()); }
/// <summary>使用线程池线程异步执行日志写入动作</summary> /// <param name="e"></param> protected virtual void PerformWriteLog(WriteLogEventArgs e) { lock (Log_Lock) { try { // 初始化日志读写器 if (LogWriter == null) InitLog(); // 写日志 LogWriter.WriteLine(e.ToString()); // 声明自动关闭日志读写器的定时器。无限延长时间,实际上不工作 if (_Timer == null) _Timer = new Timer(CloseWriter, null, Timeout.Infinite, Timeout.Infinite); // 改变定时器为5秒后触发一次。如果5秒内有多次写日志操作,估计定时器不会触发,直到空闲五秒为止 _Timer.Change(5000, Timeout.Infinite); } catch { } } }