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

            FileSystemHelpers.Instance = fs;

            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"
                          );
            fs.AddLogFile("log-2.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"
                          );

            var env    = new ApplicationLogsTestEnvironment();
            var reader = new ApplicationLogsReader(env, Mock.Of <ITracer>());

            var results = reader.GetRecentLogs(6).ToList();

            Assert.Equal(6, results.Count);
            results[0].AssertLogEntry("2013-12-06T00:29:22+00:00", "Error", "this is an error");
            results[1].AssertLogEntry("2013-12-06T00:29:22+00:00", "Error", "this is an error");
            results[2].AssertLogEntry("2013-12-06T00:29:21+00:00", "Warning", "this is a warning");
            results[3].AssertLogEntry("2013-12-06T00:29:21+00:00", "Warning", "this is a warning");
            results[4].AssertLogEntry("2013-12-06T00:29:20+00:00", "Information", "this is a log");
            results[5].AssertLogEntry("2013-12-06T00:29:20+00:00", "Information", "this is a log");
        }
コード例 #2
0
        public void ApplicationLogsReaderWillNotOpenUnlimitedNumberOfFiles()
        {
            // This test would preferrably use a mocked file system but the System.IO.Abstractions.TestHelpers have
            // some issues with returning the correct LastWrittenUtc and so this needs to run against a real file system for now.
            var fs = new FileSystem();

            FileSystemHelpers.Instance = fs;

            using (var dir = new TemporaryApplicationLogDirectory(fs))
            {
                var logFileCount = 100;
                var timestamp    = DateTimeOffset.UtcNow;
                for (int i = 0; i < logFileCount; i++)
                {
                    timestamp = timestamp.AddSeconds(1);
                    dir.AddLogFile(
                        string.Format("log-{0}.txt", i),
                        string.Format("{0:s}  PID[20108] Information this is a log\r\n", timestamp)
                        );
                }

                var env    = new ApplicationLogsTestEnvironment(dir.RootDir);
                var reader = new ApplicationLogsReader(env, Mock.Of <ITracer>());

                var results = reader.GetRecentLogs(logFileCount).ToList();

                // In this degenerate case with a large number of log files with only one line each, the limit
                // on the number of log files that can be read will be hit and so not all 100 log entries will
                // be returned.
                Assert.Equal(ApplicationLogsReader.FileOpenLimit, results.Count);
                results[0].AssertLogEntry(timestamp.ToString("s"), "Information", "this is a log");
            }
        }
コード例 #3
0
        public void ApplicationLogsReaderReturnsTheCorrectResultsWhenAccessedByMultipleThreads()
        {
            var fs = new FileSystem();

            FileSystemHelpers.Instance = fs;

            using (var dir = new TemporaryApplicationLogDirectory(fs))
            {
                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
2013-12-06T00:29:22  PID[20108] Error       this is an error"
                               );

                dir.AddLogFile("log-2.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"
                               );

                dir.AddLogFile("log-3.txt", @"2014-01-09T00:18:30 NOT A VALID LOG FILE");

                dir.AddLogFile("abc-123-logging-errors.txt",
                               @"2014-01-09T00:18:30
System.ApplicationException: The trace listener AzureTableTraceListener is disabled. 
   at Microsoft.WindowsAzure.WebSites.Diagnostics.AzureTableTraceListener.GetCloudTableClientFromEnvironment()
   at Microsoft.WindowsAzure.WebSites.Diagnostics.AzureTableTraceListener.RefreshConfig()
   --- End of inner exception stack trace ---"
                               );

                dir.AddLogFile("log-4.txt",
                               @"2013-12-06T00:29:23  PID[20108] Information this is a log
2013-12-06T00:29:24  PID[20108] Information this is a log
2013-12-06T00:29:25  PID[20108] Information this is a log
2013-12-06T00:29:26  PID[20108] Information this is a log
2013-12-06T00:29:27  PID[20108] Information this is a log
2013-12-06T00:29:28  PID[20108] Information this is a log
2013-12-06T00:29:29  PID[20108] Warning     this is a warning
2013-12-06T00:29:30  PID[20108] Error       this is an error"
                               );

                var env    = new ApplicationLogsTestEnvironment(dir.RootDir);
                var reader = new ApplicationLogsReader(env, Mock.Of <ITracer>());

                var loopResult = Parallel.For(0, 10, (i) =>
                {
                    var results = reader.GetRecentLogs(20).ToList();
                    Assert.Equal(14, results.Count);
                });

                Assert.True(loopResult.IsCompleted);
            }
        }
コード例 #4
0
        public void ApplicationLogsReaderMissingLogMessageIsValidEntry()
        {
            var fs = new ApplicationLogsTestFileSystem();

            FileSystemHelpers.Instance = fs;

            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:21  PID[20108] Warning 
2013-12-06T00:29:22  PID[20108] Error       this is an error"
                          );

            var env    = new ApplicationLogsTestEnvironment();
            var reader = new ApplicationLogsReader(env, Mock.Of <ITracer>());

            var results = reader.GetRecentLogs(6).ToList();

            Assert.Equal(4, results.Count);
            results[1].AssertLogEntry("2013-12-06T00:29:21+00:00", "Warning", "");
        }
コード例 #5
0
        public void ApplicationLogsReaderStartsWithMostRecentlyWrittenFile()
        {
            var fs = new ApplicationLogsTestFileSystem();

            FileSystemHelpers.Instance = fs;

            fs.AddLogFile("log-1.txt",
                          @"2013-12-06T00:29:20  PID[20108] Information this is the most recent log",
                          DateTimeOffset.Parse("2013-12-06T00:29:22+00:00")
                          );
            fs.AddLogFile("log-2.txt",
                          @"2013-12-06T00:29:20  PID[20108] Information this is a log",
                          DateTimeOffset.Parse("2013-12-06T00:29:20+00:00")
                          );

            var env    = new ApplicationLogsTestEnvironment();
            var reader = new ApplicationLogsReader(env, Mock.Of <ITracer>());

            var results = reader.GetRecentLogs(1).ToList();

            Assert.Equal(1, results.Count);
            results[0].AssertLogEntry("2013-12-06T00:29:20+00:00", "Information", "this is the most recent log");
        }
コード例 #6
0
        public void ApplicationLogsReaderIncompleteLogEntryIsTreatedAsContentsOfLastValidEntry()
        {
            // This is really just a variation on the multiline tests but is here to demonstrate that any 'junk' lines
            // will just be treated as part of the preceeding log message

            var fs = new ApplicationLogsTestFileSystem();

            FileSystemHelpers.Instance = fs;

            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:21  PID[20108]
2013-12-06T00:29:22  PID[20108] Error       this is an error"
                          );

            var env    = new ApplicationLogsTestEnvironment();
            var reader = new ApplicationLogsReader(env, Mock.Of <ITracer>());

            var results = reader.GetRecentLogs(6).ToList();

            Assert.Equal(3, results.Count);
        }