示例#1
0
        public void Write(string category, string source, LogType logType, string logMsg, string detail)
        {
            Guid id = Guid.NewGuid();

            LogEntity log = new LogEntity();

            log.Id       = id;
            log.Category = category;
            log.Source   = source;
            log.Type     = (short)logType;
            log.Message  = logMsg;

            LogDetailEntity logDetail = new LogDetailEntity();

            logDetail.Id        = id;
            logDetail.LogDetail = detail;

            try
            {
                using (ILHDBTran tran = BeginTran())
                {
                    LogManager manager = new LogManager(tran);
                    manager.AddLog(log);
                    manager.AddLogDetail(logDetail);
                    tran.Commit();
                }
            }
            catch (Exception ex)
            {
                FileLogWriter writer = new FileLogWriter();
                writer.Write(LogCategory.LogWrite, "Write log to database", ex);
                writer.Write(category, source, logType, logMsg, detail);
            }
        }
示例#2
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);
        }
示例#3
0
        public void Writer_Works()
        {
            var expected = LogReader.Read(InputPath1);

            var testPath = "writer-test.txt";

            FileLogWriter.Write(testPath, Input1Logs);
            var result = LogReader.Read(testPath);

            CollectionAssert.AreEqual(expected, result);
        }
示例#4
0
        public void Write(string category, string source, Exception exception)
        {
            if (exception == null)
            {
                return;
            }
            Guid id = Guid.NewGuid();

            LogEntity log = new LogEntity();

            log.Id       = id;
            log.Category = category;
            log.Source   = source;
            log.Type     = (short)LogType.Error;
            log.Message  = exception.Message;

            LogDetailEntity logDetail = new LogDetailEntity();

            logDetail.Id        = id;
            logDetail.LogDetail = LogHelper.GetExceptionMessage(exception);

            try
            {
                using (ILHDBTran tran = BeginTran())
                {
                    LogManager manager = new LogManager(tran);
                    manager.AddLog(log);
                    manager.AddLogDetail(logDetail);
                    tran.Commit();
                }
            }
            catch (Exception ex)
            {
                FileLogWriter writer = new FileLogWriter();
                writer.Write(LogCategory.LogWrite, "Write log to database", ex);
                writer.Write(category, source, exception);
            }
        }
示例#5
0
            ILogWriter ILogWriter.InheritWriter()
            {
                if (MyAPIGateway.Utilities == null)
                {
                    return(this);
                }

                ILogWriter writer = new FileLogWriter();

                foreach (var message in _pendingMessages)
                {
                    writer.Write(message);
                }
                _pendingMessages.Clear();

                return(writer);
            }
示例#6
0
        public void FallbackWriteTest()
        {
            FileLogWriter target;
            //FileLogWriter_Accessor accessor;
            TestLogWriter fallbackWriter = new TestLogWriter();

            target = new FileLogWriter(Path.Combine(TestContext.DeploymentDirectory, "testlog.txt"), LogSyncMode.Message, fallbackWriter);

            // Writing should be successful, fallback write should not be called.
            target.WriteLine("Hello");
            Assert.IsFalse(fallbackWriter.FlushWasInvoked);
            Assert.IsFalse(fallbackWriter.WriteLineWasInvoked);
            Assert.IsFalse(fallbackWriter.WriteWasInvoked);

            target = new FileLogWriter("c:\\this_dir_does_not_exist\\testlog.txt", LogSyncMode.Message, fallbackWriter);
            // First time printing will cause an exception and will print to the fallback writer.
            target.WriteLine("Hello");
            Assert.IsTrue(fallbackWriter.WriteWasInvoked);

            // At this point the fallback writer has an exception message from the Open operation.
            fallbackWriter.ResetInvocationIndicators();
            fallbackWriter.ClearOutput();

            target.WriteLine("Hello");
            string expected = "Hello" + "$n";
            string actual   = fallbackWriter.Output.ToString().Replace(Environment.NewLine, "$n");

            Assert.AreEqual(expected, actual);

            fallbackWriter.ResetInvocationIndicators();
            fallbackWriter.ClearOutput();

            target.Write("Hello");
            expected = "Hello";
            actual   = fallbackWriter.Output.ToString().Replace(Environment.NewLine, "$n");
            Assert.AreEqual(expected, actual);
        }
示例#7
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);
        }
示例#8
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);
        }
示例#9
0
        public void WorksWithoutArchiver()
        {
            Writer = new FileLogWriter(FileInfo, Formatter.Object, null, FileService.Object, DirectoryServie.Object);

            Writer.Write(LogInfo);
        }