コード例 #1
0
        public void ResumableLogReaderSeveralSmallBatchesAreEquivalentToOneLargeBatchInTheCaseOfSingleLogFile()
        {
            var fs = new ApplicationLogsTestFileSystem();

            FileSystemHelpers.Instance = fs;

            var logFile = fs.AddLogFile("log-1.txt",
                                        @"2013-12-06T00:29:20  PID[20108] Information this is a log
2013-12-06T00:29:21  PID[20108] Warning     this is a warning
2013-12-06T00:29:22  PID[20108] Error       this is an error"
                                        );

            using (var reader = new ApplicationLogsReader.ResumableLogFileReader(logFile, Mock.Of <ITracer>()))
                using (var reader2 = new ApplicationLogsReader.ResumableLogFileReader(logFile, Mock.Of <ITracer>()))
                {
                    var result1 = reader.ReadNextBatch(3).ToList();
                    var result2 = reader2.ReadNextBatch(1)
                                  .Concat(reader2.ReadNextBatch(1))
                                  .Concat(reader2.ReadNextBatch(1))
                                  .ToList();

                    Assert.Equal(reader.LastTime, reader2.LastTime);
                    Assert.Equal(3, result1.Count);
                    Assert.Equal(3, result2.Count);
                    Assert.Equal(result1[0].TimeStamp, result2[0].TimeStamp);
                    Assert.Equal(result1[1].TimeStamp, result2[1].TimeStamp);
                    Assert.Equal(result1[2].TimeStamp, result2[2].TimeStamp);
                }
        }
コード例 #2
0
        public void ResumableLogReaderCanReadFromFilesOpenForWriting()
        {
            var fs = new FileSystem();

            FileSystemHelpers.Instance = fs;

            using (var dir = new TemporaryApplicationLogDirectory(fs))
            {
                var logFile = dir.AddLogFile("log-1.txt", "2013-12-06T00:29:20  PID[20108] Information this is a log\r\n");
                using (var writer = new StreamWriter(fs.File.Open(logFile.FullName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                {
                    AutoFlush = true
                })
                    using (var reader = new ApplicationLogsReader.ResumableLogFileReader(logFile, Mock.Of <ITracer>()))
                    {
                        writer.WriteLine("2013-12-06T00:29:21  PID[20108] Warning     this is a warning");
                        var results = reader.ReadNextBatch(1);
                        Assert.Equal(1, results.Count);
                        results[0].AssertLogEntry("2013-12-06T00:29:21+00:00", "Warning", "this is a warning");

                        writer.WriteLine("2013-12-06T00:29:22  PID[20108] Error       this is an error");
                        results = reader.ReadNextBatch(1);
                        Assert.Equal(1, results.Count);
                        results[0].AssertLogEntry("2013-12-06T00:29:20+00:00", "Information", "this is a log");
                    }
            }
        }
コード例 #3
0
 public void ResumableLogReaderWillNotEnumerateEntireFile()
 {
     using (var reader = new ApplicationLogsReader.ResumableLogFileReader(DateTimeOffset.UtcNow, InfiniteLines))
     {
         var results = reader.ReadNextBatch(100);
         Assert.Equal(100, results.Count);
     }
 }
コード例 #4
0
        public void ResumableLogReaderLastTimeIsSetToTimeOfLastReadLogEntry()
        {
            var fs = new ApplicationLogsTestFileSystem();

            FileSystemHelpers.Instance = fs;

            var logFile = fs.AddLogFile("log-1.txt",
                                        @"2013-12-06T00:29:20  PID[20108] Information this is a log
2013-12-06T00:29:21  PID[20108] Warning     this is a warning
2013-12-06T00:29:22  PID[20108] Error       this is an error"
                                        );

            using (var reader = new ApplicationLogsReader.ResumableLogFileReader(logFile, Mock.Of <ITracer>()))
            {
                reader.ReadNextBatch(2);
                Assert.Equal(DateTimeOffset.Parse("2013-12-06T00:29:21+00:00"), reader.LastTime);
            }
        }
コード例 #5
0
        public void ResumableLogReaderMultilineMessagesAreMergedIntoOneEntry()
        {
            var fs = new ApplicationLogsTestFileSystem();

            FileSystemHelpers.Instance = fs;

            var logFile = fs.AddLogFile("log-1.txt",
                                        @"2013-12-06T00:29:20  PID[20108] Information this is a log
2013-12-06T00:29:21  PID[20108] Warning     this is a warning
that spans
several lines
2013-12-06T00:29:22  PID[20108] Error       this is an error"
                                        );

            using (var reader = new ApplicationLogsReader.ResumableLogFileReader(logFile, Mock.Of <ITracer>()))
            {
                var results = reader.ReadNextBatch(3);
                Assert.Equal(3, results.Count);
                results[1].AssertLogEntry("2013-12-06T00:29:21+00:00", "Warning", "this is a warning\r\nthat spans\r\nseveral lines");
            }
        }
コード例 #6
0
        public void ResumableLogReaderCanHandleFileAccessError()
        {
            var fileSystemMock = new Mock <IFileSystem>();

            fileSystemMock
            .Setup(f => f.File.Open(It.IsAny <string>(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            .Throws <UnauthorizedAccessException>();
            FileSystemHelpers.Instance = fileSystemMock.Object;

            var fileMock = new Mock <FileInfoBase>();

            var tracerMock = new Mock <ITracer>(MockBehavior.Strict);

            tracerMock.Setup(t => t.Trace("Error occurred", It.IsAny <Dictionary <string, string> >())).Verifiable();

            var reader  = new ApplicationLogsReader.ResumableLogFileReader(fileMock.Object, tracerMock.Object);
            var results = reader.ReadNextBatch(1);

            Assert.Equal(0, results.Count);
            tracerMock.Verify();
        }
コード例 #7
0
        public void ResumableLogReaderDoesNotBlockLogWriters()
        {
            var fs = new FileSystem();

            FileSystemHelpers.Instance = fs;

            using (var dir = new TemporaryApplicationLogDirectory(fs))
            {
                var logFile = dir.AddLogFile("log-1.txt",
                                             @"2013-12-06T00:29:20  PID[20108] Information this is a log
2013-12-06T00:29:21  PID[20108] Warning     this is a warning
");

                // Open a reader and read the lines while the writer appends a new line
                using (var reader = new ApplicationLogsReader.ResumableLogFileReader(logFile, Mock.Of <ITracer>()))
                {
                    var results = reader.ReadNextBatch(1);
                    Assert.Equal(1, results.Count);
                    results[0].AssertLogEntry("2013-12-06T00:29:21+00:00", "Warning", "this is a warning");

                    using (var writer = new StreamWriter(fs.File.Open(logFile.FullName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)))
                    {
                        writer.WriteLine("2013-12-06T00:29:22  PID[20108] Error       this is an error");
                    }

                    results = reader.ReadNextBatch(1);
                    Assert.Equal(1, results.Count);
                    results[0].AssertLogEntry("2013-12-06T00:29:20+00:00", "Information", "this is a log");
                }

                // Open a new reader and confirm that the new line written by the writer is present
                using (var reader = new ApplicationLogsReader.ResumableLogFileReader(logFile, Mock.Of <ITracer>()))
                {
                    var results = reader.ReadNextBatch(1);
                    Assert.Equal(1, results.Count);
                    results[0].AssertLogEntry("2013-12-06T00:29:22+00:00", "Error", "this is an error");
                }
            }
        }
コード例 #8
0
        public void ResumableLogReaderLogEntriesAreReturnedInReverseOrder()
        {
            var fs = new ApplicationLogsTestFileSystem();

            FileSystemHelpers.Instance = fs;

            var logFile = fs.AddLogFile("log-1.txt",
                                        @"2013-12-06T00:29:20  PID[20108] Information this is a log
2013-12-06T00:29:21  PID[20108] Warning     this is a warning
2013-12-06T00:29:22  PID[20108] Error       this is an error"
                                        );

            using (var reader = new ApplicationLogsReader.ResumableLogFileReader(logFile, Mock.Of <ITracer>()))
            {
                var entry1 = reader.ReadNextBatch(1).Single();
                var entry2 = reader.ReadNextBatch(1).Single();
                var entry3 = reader.ReadNextBatch(1).Single();

                entry1.AssertLogEntry("2013-12-06T00:29:22+00:00", "Error", "this is an error");
                entry2.AssertLogEntry("2013-12-06T00:29:21+00:00", "Warning", "this is a warning");
                entry3.AssertLogEntry("2013-12-06T00:29:20+00:00", "Information", "this is a log");
            }
        }