public void LogFileFinderFilesAreNotTrackedOnceDeleted()
        {
            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",
                          @"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 ---"
                          );

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

            fileFinder.FindLogFiles();
            Assert.Equal(1, fileFinder.IncludedFiles.Count);
            Assert.Equal(1, fileFinder.ExcludedFiles.Count);

            fs.RemoveLogFile("log-1.txt");
            fs.RemoveLogFile("log-2.txt");

            fileFinder.FindLogFiles();
            Assert.Equal(0, fileFinder.IncludedFiles.Count);
            Assert.Equal(0, fileFinder.ExcludedFiles.Count);
        }
        public void LogFileFinderValidFilesAreOnlyReadOnce()
        {
            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"
                          );

            var stats      = new ApplicationLogsReader.LogFileAccessStats();
            var env        = new ApplicationLogsTestEnvironment();
            var fileFinder = new ApplicationLogsReader.LogFileFinder(env, Mock.Of <ITracer>(), stats);

            fileFinder.FindLogFiles();

            fs.AddLogFile("log-2.txt", @"2013-12-06T00:29:20  PID[20108] Information this is a log");
            fileFinder.FindLogFiles();

            fs.AddLogFile("log-3.txt", @"2013-12-06T00:29:20  PID[20108] Information this is a log");
            var results = fileFinder.FindLogFiles().ToList();

            Assert.Equal(3, results.Count);
            Assert.Equal(1, stats.GetOpenTextCount("log-1.txt"));
            Assert.Equal(1, stats.GetOpenTextCount("log-2.txt"));
            Assert.Equal(1, stats.GetOpenTextCount("log-3.txt"));
        }
        public void LogFileFinderIgnoredFilesAreOnlyReadOnce()
        {
            var fs = new ApplicationLogsTestFileSystem();

            FileSystemHelpers.Instance = fs;
            fs.AddLogFile("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 ---"
                          );

            var stats      = new ApplicationLogsReader.LogFileAccessStats();
            var env        = new ApplicationLogsTestEnvironment();
            var fileFinder = new ApplicationLogsReader.LogFileFinder(env, Mock.Of <ITracer>(), stats);
            var results    = fileFinder.FindLogFiles().ToList();

            Assert.Equal(0, results.Count);
            Assert.Equal(1, stats.GetOpenTextCount("logging-errors.txt"));

            results = fileFinder.FindLogFiles().ToList();

            Assert.Equal(0, results.Count);
            Assert.Equal(1, stats.GetOpenTextCount("logging-errors.txt"));
        }
        public void LogFileFinderEmptyFilesAreReadEachTime()
        {
            var fs = new ApplicationLogsTestFileSystem();

            FileSystemHelpers.Instance = fs;
            fs.AddLogFile("log-1.txt", "");

            var stats      = new ApplicationLogsReader.LogFileAccessStats();
            var env        = new ApplicationLogsTestEnvironment();
            var fileFinder = new ApplicationLogsReader.LogFileFinder(env, Mock.Of <ITracer>(), stats);

            fileFinder.FindLogFiles();
            fileFinder.FindLogFiles();
            Assert.Equal(2, stats.GetOpenTextCount("log-1.txt"));
        }
        public void LogFileFinderCanHandleDirectoryDoesNotExist()
        {
            var fs = new ApplicationLogsTestFileSystem();

            FileSystemHelpers.Instance = fs;
            var env        = new ApplicationLogsTestEnvironment();
            var fileFinder = new ApplicationLogsReader.LogFileFinder(env, Mock.Of <ITracer>());
            var results    = fileFinder.FindLogFiles().ToList();

            Assert.Equal(0, results.Count);
        }
        public void LogFileFinderNoLogFilesFoundForEmptyDirectory()
        {
            var fs = new ApplicationLogsTestFileSystem();

            FileSystemHelpers.Instance = fs;
            var env = new ApplicationLogsTestEnvironment();

            fs.AddDirectory(env.ApplicationLogFilesPath);
            var fileFinder = new ApplicationLogsReader.LogFileFinder(env, Mock.Of <ITracer>());
            var results    = fileFinder.FindLogFiles().ToList();

            Assert.Equal(0, results.Count);
        }
        public void LogFileFinderEmptyFilesAreNotReturned()
        {
            var fs = new ApplicationLogsTestFileSystem();

            FileSystemHelpers.Instance = fs;
            fs.AddLogFile("log-1.txt", "");

            var env        = new ApplicationLogsTestEnvironment();
            var fileFinder = new ApplicationLogsReader.LogFileFinder(env, Mock.Of <ITracer>());
            var results    = fileFinder.FindLogFiles();

            Assert.Equal(0, results.Count());
            Assert.Equal(0, fileFinder.IncludedFiles.Count);
            Assert.Equal(0, fileFinder.ExcludedFiles.Count);
        }
        public void LogFileFinderFindsSingleLogFile()
        {
            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"
                          );

            var env        = new ApplicationLogsTestEnvironment();
            var fileFinder = new ApplicationLogsReader.LogFileFinder(env, Mock.Of <ITracer>());
            var results    = fileFinder.FindLogFiles().ToList();

            Assert.Equal(1, results.Count);
            Assert.Equal(1, fileFinder.IncludedFiles.Count);
        }
        public void LogFileFinderSkipsFileOnFileAccessError()
        {
            var fs = new ApplicationLogsTestFileSystem();

            FileSystemHelpers.Instance = fs;
            fs.AddLogFileWithOpenException <UnauthorizedAccessException>("log-1.txt");
            var env = new ApplicationLogsTestEnvironment();

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

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

            var fileFinder = new ApplicationLogsReader.LogFileFinder(env, tracerMock.Object);
            var results    = fileFinder.FindLogFiles().ToList();

            Assert.Equal(0, results.Count);
            Assert.Equal(0, fileFinder.IncludedFiles.Count);
            Assert.Equal(0, fileFinder.ExcludedFiles.Count);
            tracerMock.Verify();
        }