Esempio n. 1
0
        public void TestMethodDLogMemlog()
        {
            ThreadPool.SetMinThreads(32, 32);

            int ThreadStartCount = 0;
            int DoneCount        = 0;

            DLog.Close();//关闭

            string logDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log");

            if (Directory.Exists(logDir))
            {
                Directory.Delete(logDir, true);
            }


            DLog.ConsoleLogEnable(false);//禁用控制台,如果不禁用的话时间可能很长
            DLog.Init(logDir);
            Assert.IsTrue(DLog.IsInitialized());

            Thread.Sleep(500);
            int testMsgNum = 500;

            //DLog.dlog_set_usual_thr(DLog.DLOG_ERROR + 1);

            DLog.MemoryLogEnable(true);

            for (int threadCount = 0; threadCount < 20; threadCount++)//20个线程一起写
            {
                Task.Run(() =>
                {
                    Interlocked.Increment(ref ThreadStartCount);
                    //要注意条数不能太多了,否则超出内存日志的缓存上限,会造成后面Assert条数失败
                    for (int i = 0; i < testMsgNum; i++)
                    {
                        xuexue.DLog.LogI($"测试日志{i}");
                    }
                    Interlocked.Increment(ref DoneCount);
                });
            }

            while (true)
            {
                Thread.Sleep(100);
                if (DoneCount == ThreadStartCount)
                {
                    break;
                }
            }


            int msgCount = 0;

            while (true)
            {
                string msg = DLog.GetMemorylog();
                if (!String.IsNullOrEmpty(msg))
                {
                    msgCount++;
                    Assert.IsTrue(msg.ToString().Contains("测试日志"));//检查文本内容是否有大问题
                }
                else
                {
                    break;
                }
            }

            Assert.IsTrue(msgCount == 20 * testMsgNum); //检查消息条数是否漏了

            DLog.Close();                               //必须要关闭,否则有线程在后台还
        }