public void Test_GetWriters() { WriterFactory.Config.Enable = false; var result = WriterFactory.GetWriters(typeof(int)); Assert.AreEqual(null, result); WriterFactory.Config.Enable = true; var result2 = WriterFactory.GetWriters(typeof(int)); Assert.AreEqual(null, result2); }
/// <summary> /// 供外部定时器调用,一次性写入所有等待消息 /// 此方法由定时器线程调用。 /// </summary> public void Flush() { List <T> tempList = null; lock ( _lock ) { if (_list.Count > 0) { // 将静态队列的数据转移到临时队列,避免在后面写操作时长时间占用锁 tempList = (from x in _list select x).ToList(); // 清空静态队列 _list.Clear(); } } if (tempList == null) { return; // 没有需要写入的日志信息 } // 获取写日志的实例,注意:允许一个类型配置多个写入方式 ILogWriter[] writers = WriterFactory.GetWriters(typeof(T)); // 如果类型没有配置日志序列化器,就忽略 if (writers == null || writers.Length == 0) { return; } List <Exception> exceptions = new List <Exception>(); // 临时保存写日志过程的异常 foreach (var writer in writers) { try { writer.Write(tempList); } catch (Exception ex) { exceptions.Add(ex); } } // 为了便于单元测试,所以提炼了一个内部方法 ProcessFlushException(exceptions); }