public static void Collect() { log.Info("START: DataCollector.Collect()"); try { var businessList = Database.GetBusinessLineList(null); List <Task> tasks = new List <Task>(); List <DataItem> allExternalItems = new List <DataItem>(); foreach (var biz in businessList) { log.DebugFormat("Creating data collection tasks for business (id={0}, code={1}, name={2})", biz.Id, biz.Code, biz.Name); foreach (var ln in biz.Line) { log.DebugFormat("Creating data collection task for line (id={0}, code={1}, name={2})", ln.Id, ln.Code, ln.Name); Task t = Task.Run(() => { log.DebugFormat("Starting data collection task for business/line {0}/{1}...", biz.Code, ln.Code); var items = Fetcher.FetchItems(biz.Code, ln.Code, 2); log.DebugFormat("Collected {0} items", items.Count); lock (listLocker) { allExternalItems.AddRange(items); } log.DebugFormat("Finished data collection task for business/line {0}/{1}!", biz.Code, ln.Code); var dsName = String.Format("biz-{0}-ln-{1}", biz.Code, ln.Code); ILog dsLog = LogManager.GetLogger(dsName + "Logger"); dsLog.AddFile(dsName, root: "App_Data", extension: "txt", pattern: "%m%n", level: "ALL"); dsLog.Debug(DateTime.Now.ToString()); dsLog.Debug("+----+------+--------------------+--------------------+--------------------+--------------------+--------------------+----------+"); dsLog.Debug( String.Format("|{0,-4}|{1,-6}|{2,-20}|{3,-20}|{4,-20}|{5,-20}|{6,-20}|{7,10}|", "Line", "Biz", "Entered", "Called", "CalledByName", "Serviced", "ServicedByName", "QueueId")); dsLog.Debug("+----+------+--------------------+--------------------+--------------------+--------------------+--------------------+----------+"); foreach (var item in items) { dsLog.Debug(item.ToString()); } dsLog.Debug("+----+------+--------------------+--------------------+--------------------+--------------------+--------------------+----------+"); }); tasks.Add(t); } } Task ending = Task.WhenAll(tasks); try { ending.Wait(); } catch (Exception ex) { log.ErrorFormat("Data collection task failed: {0}", ex); throw ex; } log.DebugFormat("Collected total of {0} items", allExternalItems.Count); Write(allExternalItems); //if (ending.Status == TaskStatus.RanToCompletion) // CacheEngine.Instance.State = CacheEngineState.Ready; //else if (ending.Status == TaskStatus.Faulted) // CacheEngine.Instance.State = CacheEngineState.Invalid; } catch (Exception ex) { log.ErrorFormat("DataCollector.Collect() failed: {0}", ex); } log.Info("END: DataCollector.Collect()"); }