短信平台记录日志模块,是经过异步方式来记录的,即日志工具类里初始化一个Queue对象,公共的写日志方法的处理逻辑是把日志消息放到Queue里。构造器里设定一个死循环,不停的读队,而后把日志消息持久化到磁盘的文本文件里。html
构造方法的代码以下:异步
public Log() { //开启线程负责将日志写入到指定地点 ThreadPool.QueueUserWorkItem(o => { try { while (true) { if (_messageQueue.Count > 0) { lock (_messageQueue) { while (_messageQueue.Count > 0) { LogMessage logMessage = _messageQueue.Dequeue(); WriteFile(logMessage); } } } else { Thread.Sleep(300); } } } catch (Exception ex) { LogMessage log = new LogMessage() { Content = "构造器执行报错:" + ex.ToString() }; FileHelper.WriteFile(string.Format(@"{0}\{1:yyyy-MM-dd}-LOGERROR.txt", _logPath, DateTime.Now), log.GetFormattedLog()); } }); }
近期经过看线上站点日志,发现不按期会有ThreadAbortException被捕获,分布式
时间:2016-08-02 10:23:01 构造器执行报错:System.Threading.ThreadAbortException: 正在停止线程。 在 System.Threading.Thread.SleepInternal(Int32 millisecondsTimeout) 在 Common.Log.<.ctor>b__1(Object o) ------------------------------------------------------------------------------- 时间:2016-08-02 10:47:32 构造器执行报错:System.Threading.ThreadAbortException: 正在停止线程。 在 System.Threading.Thread.SleepInternal(Int32 millisecondsTimeout) 在 Common.Log.<.ctor>b__2(Object o) ----------------------------------------------------------------------------------------
经过分析,当站点应用程序池回收或遇到其余未捕获的异常时,线程会被Abort,这时,当线程里的代码再被执行时,就会抛出ThreadAbortException异常。模块化
微软官方对ThreadAbortException的介绍:工具
在调用 Abort 方法以销毁线程时,CLR将引起ThreadAbortException。ThreadAbortException 是一种可捕获的特殊异常,但在 catch 块的结尾处它将自动被再次引起。引起此异常时,运行库将在结束线程前执行全部 finally 块。因为线程能够在 finally 块中执行未绑定计算,或调用 Thread.ResetAbort 来取消停止,因此不能保证线程将彻底结束。若是您但愿一直等到被停止的线程结束,能够调用 Thread.Join 方法。Join 是一个模块化调用,它直到线程实际中止执行时才返回。性能
下面的示例说明如何停止线程。接收 ThreadAbortException 的线程使用 ResetAbort 方法取消停止请求并继续执行。spa
using System; using System.Threading; using System.Security.Permissions; public class ThreadWork { public static void DoWork() { try { for(int i=0; i<100; i++) { Console.WriteLine("Thread - working."); Thread.Sleep(100); } } catch(ThreadAbortException e) { Console.WriteLine("Thread - caught ThreadAbortException - resetting."); Console.WriteLine("Exception message: {0}", e.Message); Thread.ResetAbort(); } Console.WriteLine("Thread - still alive and working."); Thread.Sleep(1000); Console.WriteLine("Thread - finished working."); } } class ThreadAbortTest { public static void Main() { ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork); Thread myThread = new Thread(myThreadDelegate); myThread.Start(); Thread.Sleep(100); Console.WriteLine("Main - aborting my thread."); myThread.Abort(); myThread.Join(); Console.WriteLine("Main ending."); } }
运行结果:线程
Thread - working. Main - aborting my thread. Thread - caught ThreadAbortException - resetting. Exception message: Thread was being aborted. Thread - still alive and working. Thread - finished working. Main ending.
————————————————————–我是萌萌哒分界线————————————————————日志