Esempio n. 1
0
        public static void StartWatcher(WatcherType watcherType, FileWatchConfiguration fileWatchConfiguration,
            IStreamReader streamReader, ISleeper sleeper)
        {
            var watcher = CreateWatcherInternal(watcherType, streamReader, sleeper);
            watcher.Configuration = fileWatchConfiguration;

            AddWatcherToPool(watcher);
            watcher.Start();
        }
Esempio n. 2
0
        private void SetStreamPosition(FileWatchConfiguration conf)
        {
            if (conf.NumberOfLinesToOutputWhenWatchingStarts < 1) return;

            var lineNumberToStartTailingFrom = File.ReadLines(conf.FileName).Count() -
                                               conf.NumberOfLinesToOutputWhenWatchingStarts;

            if (lineNumberToStartTailingFrom <= 0) return;

            var index = 1;

            while (_streamReader.ReadLine() != null)
            {
                if (lineNumberToStartTailingFrom == index) break;

                index++;
            }
        }
Esempio n. 3
0
 public string GetLastLinesFromFile(FileWatchConfiguration conf)
 {
     _streamReader = new StreamReader(new FileStream(conf.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
     SetStreamPosition(conf);
     return _streamReader.ReadToEnd();
 }
Esempio n. 4
0
        private static FileWatchConfiguration TailOptionsToFileWatchConfiguration(TailOptions options)
        {
            var assertionResult = AssertFileExists(options);
            if (!assertionResult.FileExists)
            {
                System.Console.WriteLine(assertionResult.AssertionFailedReason);
                Environment.Exit(0);
            }

            var conf = new FileWatchConfiguration
            {
                PollIntervalInMs = (int) options.SleepIntervalInSeconds*1000,
                Observer = new ConsoleObserver(),
                FileName = options.Filename.First(),
                NumberOfLinesToOutputWhenWatchingStarts = options.Lines
            };

            if (!string.IsNullOrWhiteSpace(options.InclusionFilter))
            {
                conf.WatchFilter = new WatchFilter
                {
                    InclusionFilter = new Filter
                    {
                        SimpleFilter = options.InclusionFilter
                    }
                };
            }

            if (!string.IsNullOrWhiteSpace(options.ExclusionFilter))
            {
                conf.WatchFilter = new WatchFilter
                {
                    ExclusionFilter = new Filter
                    {
                        SimpleFilter = options.ExclusionFilter
                    }
                };
            }

            return conf;
        }