Beispiel #1
0
        public void DeleteFileWhenInitialize()
        {
            "使用 With 初始化时,不能要求文件存在。".Test((Func <TextFileLogger, TextFileLogger> extraBuilder) =>
            {
                var testFile = PrepareNotExistFile("test21.md");

                var aLogger = extraBuilder(new TextFileLogger(new FileInfo(testFile.FullName)));
                aLogger.Message("YY");
                aLogger.Close();

                // 无异常。
            }).WithArguments(
                x => x.WithMaxFileSize(100),
                x => x.WithMaxLineCount(100),
                x => x.WithMaxLineCount(100, 50),
                x => x.WithWholeFileOverride()
                );

            "初始化时,超过大小的文件内容会清空。".Test(() =>
            {
                const string testFile = "test22.md";
                File.WriteAllText(testFile, "XXXXXXXX\n");

                var aLogger = new TextFileLogger(new FileInfo(testFile))
                              .WithMaxFileSize(4);
                aLogger.Message("YY");
                aLogger.Close();

                var lines = File.ReadAllLines(testFile);
                Assert.AreEqual(1, lines.Length);
                Assert.IsTrue(lines[0].Contains("YY"));
            });

            "初始化时,超过行数的文件前面行会删除。".Test(() =>
            {
                const string testFile = "test23.md";
                File.WriteAllText(testFile, "XXXXXXXX\n\nYYYYYYYY\nZZZZZZZZ\n");

                var aLogger = new TextFileLogger(new FileInfo(testFile))
                              .WithMaxLineCount(3, 1);
                aLogger.Message("WW");
                aLogger.Close();

                var lines = File.ReadAllLines(testFile);
                Assert.AreEqual(2, lines.Length);
                Assert.IsTrue(lines[0].Contains("ZZZZZZZZ"));
            });
        }
Beispiel #2
0
 public void Stop()
 {
     _watcher.EnableRaisingEvents = false;
     _queueTaskWorker.Stop();
     _logger.Close();
 }