コード例 #1
0
        public void TestMultithreadWriteToLogFile()
        {
            var tmpFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData)
                                                             + @"\" + Guid.NewGuid().ToString();

            var conf = new MonitorConfiguration()
            {
                LogLevel = "Info",
                LogFilePath = tmpFolder + @"\test.log"
            };

            var logWriter = new FileLogWriter(conf);

            Task[] tasks = new Task[100];
            for(int i = 0; i < tasks.Length; i++)
            {
                tasks[i] = Task.Run(() =>
                {
                    Console.WriteLine(Process.GetCurrentProcess().Threads.Count);
                    String logContent = Guid.NewGuid().ToString();
                    logWriter.Write(LogLevel.Info, logContent);

                });
            }
            Task.WaitAll(tasks);
            Directory.Delete(tmpFolder, true);
        }
コード例 #2
0
        public void TestRotateLog()
        {
            var tmpFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData)
                                                             + @"\" + Guid.NewGuid().ToString();

            Console.WriteLine(tmpFolder);
            Directory.CreateDirectory(tmpFolder);
            var conf = new MonitorConfiguration()
            {
                LogLevel = "Info",
                LogFilePath = tmpFolder + @"\test.log",
                LogFileSize = 1,
                MaxLogRetention = 2
            };

            var logWriter = new FileLogWriter(conf);

            String[] logContent = new String[4]
            {
                "Test 0","Test 1","Test 2","Test 3",
            };

            //echo "Test 0" > test.log
            logWriter.Write(LogLevel.Info, logContent[0]);
            //mv test.log test.log.0
            //echo "Test 1" > test.log
            logWriter.Write(LogLevel.Info, logContent[1]);

            Assert.IsTrue(File.Exists(conf.LogFilePath));
            String log = File.ReadAllText(conf.LogFilePath);
            Assert.IsTrue(log.Contains(logContent[1]));

            Assert.IsTrue(File.Exists(conf.LogFilePath + @".0"));
            log = File.ReadAllText(conf.LogFilePath + @".0");
            Assert.IsTrue(log.Contains(logContent[0]));

            //mv test.log.0  test.log.1
            //mv test.log test.log.0
            //echo "Test 2" > test.log
            logWriter.Write(LogLevel.Info, logContent[2]);

            //rm test.log.1
            //mv test.log.0 test.log.1
            //mv test.log test.log.0
            //echo "Test 3" test.log
            logWriter.Write(LogLevel.Info, logContent[3]);

            Assert.IsTrue(File.Exists(conf.LogFilePath + @".0"));
            Assert.IsTrue(File.Exists(conf.LogFilePath + @".1"));
            Assert.IsFalse(File.Exists(conf.LogFilePath + @".2"));

            log = File.ReadAllText(conf.LogFilePath);
            Assert.IsTrue(log.Contains(logContent[3]));

            log = File.ReadAllText(conf.LogFilePath + @".1");
            Assert.IsTrue(log.Contains(logContent[1]));

            Directory.Delete(tmpFolder, true);
        }
コード例 #3
0
        public void TestWriteToLogFile()
        {
            var tmpFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData)
                                                             + @"\" + Guid.NewGuid().ToString();

            var conf = new MonitorConfiguration()
            {
                LogLevel = "Info",
                LogFilePath = tmpFolder + @"\test.log"
            };

            var logWriter = new FileLogWriter(conf);

            String logContent = Guid.NewGuid().ToString();
            logWriter.Write(LogLevel.Info, logContent);

            Assert.IsTrue(File.Exists(conf.LogFilePath));
            String log = File.ReadAllText(conf.LogFilePath);
            Assert.IsTrue(log.Contains(logContent));

            Directory.Delete(tmpFolder, true);
        }