Пример #1
0
        /// <summary>输出日志</summary>
        /// <param name="msg">信息</param>
        public void WriteLine(String msg)
        {
            // 小对象,采用对象池的成本太高了
            var e = new WriteLogEventArgs(msg, null, true);

            PerformWriteLog(e);
        }
Пример #2
0
 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);
         }
     }
 }
Пример #3
0
        /// <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);
        }
Пример #4
0
        /// <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 { }
            }
        }
Пример #5
0
        /// <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);
        }
Пример #6
0
 /// <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);
     }
 }
Пример #7
0
        /// <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);
            }
        }
Пример #8
0
        /// <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);
            }
        }
Пример #9
0
 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;
     }
 }
Пример #10
0
 /// <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 { }
     }
 }
Пример #11
0
 private void ConsoleWriteLog(WriteLogEventArgs e)
 {
     var msg = e.ToString();
     Console.WriteLine(msg);
 }
Пример #12
0
        /// <summary>输出日志</summary>
        /// <param name="msg">信息</param>
        public void Write(String msg)
        {
            var e = new WriteLogEventArgs(msg, null, false);

            PerformWriteLog(e);
        }
Пример #13
0
 /// <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 { }
     }
 }
Пример #14
0
 /// <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);
 }
Пример #15
0
 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;
     }
 }
Пример #16
0
        /// <summary>输出日志</summary>
        /// <param name="msg">信息</param>
        public void Write(String msg)
        {
            var e = new WriteLogEventArgs(msg, null, false);

            PerformWriteLog(e);
        }
Пример #17
0
 private static void XTrace_OnWriteLog(object sender, WriteLogEventArgs e)
 {
     //Console.WriteLine(e.ToString());
     ConsoleWriteLog(e);
 }
Пример #18
0
 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);
     }
 }
Пример #19
0
        /// <summary>输出异常日志</summary>
        /// <param name="ex">异常信息</param>
        public void WriteException(Exception ex)
        {
            var e = new WriteLogEventArgs(null, ex, true);

            PerformWriteLog(e);
        }
Пример #20
0
        /// <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());
        }
Пример #21
0
        /// <summary>输出日志</summary>
        /// <param name="msg">信息</param>
        public void WriteLine(String msg)
        {
            // 小对象,采用对象池的成本太高了
            var e = new WriteLogEventArgs(msg, null, true);

            PerformWriteLog(e);
        }
Пример #22
0
        /// <summary>输出异常日志</summary>
        /// <param name="ex">异常信息</param>
        public void WriteException(Exception ex)
        {
            var e = new WriteLogEventArgs(null, ex, true);

            PerformWriteLog(e);
        }
Пример #23
0
        /// <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); }
        }
Пример #24
0
        /// <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());
        }
Пример #25
0
 private static void XTrace_OnWriteLog(object sender, WriteLogEventArgs e)
 {
     //Console.WriteLine(e.ToString());
     ConsoleWriteLog(e);
 }
Пример #26
0
        private void ConsoleWriteLog(WriteLogEventArgs e)
        {
            var msg = e.ToString();

            Console.WriteLine(msg);
        }
Пример #27
0
 /// <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 { }
     }
 }