private async Task CreateAndRunWatcher <TData>(string testName, string filter, IConfiguration config, Func <List <IEnvelope>, Task> testBody, IRecordParser <TData, LogContext> recordParser, ILogger logger, TestDirectory directory = null)
        {
            //Create a distinct directory based on testName so that tests can run in parallel
            string testDir = Path.Combine(TestUtility.GetTestHome(), testName);

            if (directory == null)
            {
                //The following will creates all directories and subdirectories in the specified path unless they already exist.
                Directory.CreateDirectory(testDir);

                //Clean up before the test rather than after so that we can inspect the files
                DeleteFiles(testDir, "*.*");
            }
            else
            {
                this.SetUpTestDirectory(directory, TestUtility.GetTestHome());
            }

            ListEventSink logRecords = new ListEventSink();

            DirectorySource <TData, LogContext> watcher = new DirectorySource <TData, LogContext>
                                                              (testDir, filter, 1000, new PluginContext(config, logger, null), recordParser);

            watcher.Subscribe(logRecords);
            watcher.Start();

            await testBody(logRecords);

            watcher.Stop();
        }
        private DirectorySource <IDictionary <string, string>, LogContext> CreateDirectorySource(string sourceId, string filter, ListEventSink logRecords)
        {
            DirectorySource <IDictionary <string, string>, LogContext> watcher = new DirectorySource <IDictionary <string, string>, LogContext>
                                                                                     (BookmarkDirectory,
                                                                                     filter,
                                                                                     1000,
                                                                                     new PluginContext(null, NullLogger.Instance, null),
                                                                                     new TimeStampRecordParser(RECORD_TIME_STAMP_FORMAT, null, DateTimeKind.Utc));

            watcher.Id = sourceId;
            watcher.Subscribe(logRecords);
            return(watcher);
        }
Esempio n. 3
0
        private async Task CreateAndRunWatcher <TData>(string filter, IConfiguration config, Func <List <IEnvelope>, Task> testBody, IRecordParser <TData, LogContext> recordParser)
        {
            ListEventSink logRecords = new ListEventSink();
            DirectorySource <TData, LogContext> watcher = new DirectorySource <TData, LogContext>
                                                              (TestUtility.GetTestHome(), filter, 1000, new PluginContext(config, NullLogger.Instance, null), recordParser, DirectorySourceFactory.CreateLogSourceInfo);

            watcher.Subscribe(logRecords);
            watcher.Start();

            await testBody(logRecords);

            watcher.Stop();
        }
Esempio n. 4
0
        public async Task TestJsonLog(string sourceId)
        {
            var config = TestUtility.GetConfig("Sources", sourceId);
            var dir    = config["Directory"];

            Directory.CreateDirectory(dir);
            var logFile = Path.Combine(dir, $"{Guid.NewGuid()}.log");

            File.WriteAllText(logFile, LOG);

            try
            {
                var source = new DirectorySource <JObject, LogContext>(dir, config["FileNameFilter"],
                                                                       1000, new PluginContext(config, NullLogger.Instance, null),
                                                                       new SingleLineJsonParser(config["TimestampField"], config["TimestampFormat"], NullLogger.Instance))
                {
                    InitialPosition = InitialPositionEnum.BOS
                };
                var sink = new ListEventSink();
                source.Subscribe(sink);
                source.Start();

                await Task.Delay(2000);

                Assert.Equal(2, sink.Count);

                var record0 = sink[0] as LogEnvelope <JObject>;
                Assert.Equal(new DateTime(2018, 9, 21, 8, 38, 50, 972), record0.Timestamp);
                Assert.Equal("UserProfile", record0.Data["ul-log-data"]["method_name"]);
                Assert.Equal("INFO", record0.Data["ul-tag-status"]);
            }
            finally
            {
                File.Delete(logFile);
            }
        }