예제 #1
0
파일: UnitTest1.cs 프로젝트: daixian/dlog
        public void TestMethodDLogInitClose()
        {
            DLog.Close();
            Assert.IsFalse(DLog.IsInitialized());
            string logDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log");

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

            int count = 0;
            int res   = 0;

            for (int i = 0; i < 50; i++)
            {
                res = DLog.Init(logDir, $"openclose{count}");
                Assert.IsTrue(DLog.IsInitialized());
                Assert.IsTrue(res == 0);
                DLog.LogI($"开关测试log!{count}");
                DLog.Close();
                count++;
            }

            for (int i = 0; i < 50; i++)
            {
                res = DLog.Init(logDir, $"开关测试{count}");
                Assert.IsTrue(res == 0);
                DLog.LogI($"开关测试log!{count}");
                res = DLog.Init(logDir, $"开关测试{count}");
                Assert.IsTrue(res == 1);
                DLog.LogI($"开关测试log!{count}");
                DLog.Close();
                count++;
            }


            for (int i = 0; i < 50; i++)
            {
                DLog.Init(logDir, $"开关测试{count}");
                DLog.LogI($"开关测试log!{count}");
                DLog.Init(logDir, $"开关测试{count}");
                DLog.LogI($"开关测试log!{count}");
                DLog.Init(logDir, $"开关测试{count}");
                DLog.Close();
                count++;
            }

            for (int i = 0; i < 50; i++)
            {
                DLog.Init(logDir, $"开关测试{count}");
                DLog.LogI($"开关测试log!{count}");
                DLog.Init(logDir, $"开关测试二{count}");
                DLog.LogI($"开关测试log!{count}");
                DLog.Init(logDir, $"开关测试二{count}");
                DLog.LogI($"开关测试log!{count}");
                DLog.Close();
                DLog.Close();
                count++;
            }

            for (int i = 0; i < 50; i++)
            {
                DLog.Init(logDir, $"开关测试{count}");
                DLog.LogI($"开关测试log!{count}");
                DLog.Close();
                DLog.Close();
                count++;
            }

            string[] logfiles = Directory.GetFiles(logDir);
            Assert.IsTrue(logfiles.Length == count);

            DLog.Close();//关闭
        }
예제 #2
0
파일: UnitTest1.cs 프로젝트: daixian/dlog
        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();                               //必须要关闭,否则有线程在后台还
        }