示例#1
0
        public void DatebaseLogAndStatus()
        {
            var    logger = DLog.GetLogger();
            string id     = Guid.NewGuid().ToString("N");

            Env.NodeId = "DEFAULT";
            using (Spider spider = Spider.Create(new Site {
                EncodingName = "UTF-8"
            },
                                                 id,
                                                 new QueueDuplicateRemovedScheduler(),
                                                 new TestPageProcessor()))
            {
                spider.Downloader = new TestDownloader();
                spider.TaskId     = "1";
                spider.Monitor    = new MySqlMonitor(spider.TaskId, spider.Identity, false, "Database='mysql';Data Source=localhost;User ID=root;Port=3306;SslMode=None;");
                spider.AddPipeline(new TestPipeline());
                for (int i = 0; i < 5; i++)
                {
                    logger.NLog(id, "add start url" + i, Level.Info);
                    spider.AddStartUrl("http://www.baidu.com/" + i);
                }
                spider.EmptySleepTime = 1000;
                spider.Run();
            }
            using (var conn = new MySqlConnection("Database='mysql';Data Source=localhost;User ID=root;Port=3306;SslMode=None;"))
            {
                var logs = conn.Query <Log>($"SELECT * FROM dotnetspider.log where identity='{id}'").ToList();
                Assert.StartsWith("Crawl complete, cost", logs[logs.Count - 1].message);
                Assert.Equal(1, conn.Query <CountResult>($"SELECT COUNT(*) as Count FROM dotnetspider.status where identity='{id}'").First().Count);
                Assert.Equal("Finished", conn.Query <statusObj>($"SELECT * FROM dotnetspider.status where identity='{id}'").First().status);
            }
        }