Esempio n. 1
0
        public async Task FindFiles(LogParserModel model)
        {
            if (!string.IsNullOrEmpty(model.Paths))
            {
                try
                {
                    var result = await Task.Run(() => GetFiles(model));

                    result.Insert(0, $"Total files found:{result.Count}");
                    result.Add("---------END---------");
                    model.ResultDisplay = result;
                }

                catch (Exception ex)
                {
                    model.ResultDisplay = new List <string> {
                        ex.ToString()
                    };
                }
            }

            else
            {
                model.ResultDisplay = new List <string> {
                    "Empty paths"
                }
            };
        }
Esempio n. 2
0
        private List <string> GetFiles(LogParserModel model)
        {
            var paths = model.Paths.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Distinct().ToArray();

            var masks = model.Masks?.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Distinct().ToArray();

            var list = new List <string>();

            foreach (var path in paths)
            {
                var searchOption = model.IncludeSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;

                if (masks != null && masks.Length > 0)
                {
                    foreach (var mask in masks)
                    {
                        var files = Directory.GetFiles(path, mask, searchOption);
                        list.AddRange(files);
                    }
                }

                else
                {
                    var files = Directory.GetFiles(path, "*", searchOption);
                    list.AddRange(files);
                }
            }

            return(list.Distinct().ToList());
        }
Esempio n. 3
0
        private List <string> ProcessFiles(LogParserModel model)
        {
            _cts = new CancellationTokenSource();
            var token = _cts.Token;

            var includeFileInfo = model.IncludeFileInfo;

            var result = new List <string>();

            try
            {
                var files = GetFiles(model);

                string searchString = model.SearchLine.ToLower();

                var bag = new ConcurrentBag <LineInfo>();

                Parallel.ForEach(files, new ParallelOptions {
                    CancellationToken = token
                }, file =>
                {
                    ProcessFile(file, searchString, bag, includeFileInfo);
                });

                result = bag
                         .OrderBy(x => x.FilePath)
                         .ThenBy(y => y.RowNumber)
                         .Select(g => g.Line)
                         .ToList();

                result.Insert(0, $"Found {bag.Count} times\n\n");
            }

            catch (OperationCanceledException)
            {
                result = new List <string> {
                    $"Search has been canceled"
                };
            }

            catch (Exception ex)
            {
                result = new List <string> {
                    $"ERROR: {ex}"
                };
            }

            finally
            {
                if (_cts != null)
                {
                    _cts.Dispose();
                    _cts = null;
                }
            }

            return(result);
        }
Esempio n. 4
0
        public void TestEmptyPaths()
        {
            var manager = new LogParserManager();
            var model   = new LogParserModel();

            manager.FindFiles(model).GetAwaiter().GetResult();
            Assert.AreEqual(1, model.ResultDisplay.Count);
            Assert.AreEqual("Empty paths", model.ResultDisplay[0]);
        }
Esempio n. 5
0
        public void AssertFilesCount()
        {
            var manager = new LogParserManager();
            var model   = new LogParserModel();

            model.IncludeFileInfo = false;
            var dir = Directory.GetCurrentDirectory();

            model.Paths = $"{dir}\\Data";
            manager.FindFiles(model).GetAwaiter().GetResult();
            Assert.AreEqual(4, model.ResultDisplay.Count);
            Assert.AreEqual($"{model.Paths}\\example20190101.log", model.ResultDisplay[1]);
            Assert.AreEqual($"{model.Paths}\\example20200101.log", model.ResultDisplay[2]);
        }
Esempio n. 6
0
        public async Task Search(LogParserModel model)
        {
            if (model.Validate())
            {
                Stopwatch elapsedTime = new Stopwatch();
                elapsedTime.Start();

                var result = await Task.Run(() => ProcessFiles(model));

                model.ResultDisplay = result;

                elapsedTime.Stop();
                model.ElapsedTime = $"Elapsed time: {elapsedTime.Elapsed.ToString()}";
            }
        }
Esempio n. 7
0
        public LogParserViewModel(LogParserManager manager)
        {
            _manager = manager;
            Model    = new LogParserModel();

            var cleanDisplay = ReactiveCommand.Create(() =>
            {
                Model.ResultDisplay = new List <string> {
                    string.Empty
                };
                Model.ElapsedTime = "Elapsed time: -/-";
            });

            Search    = ReactiveCommand.CreateFromTask(async() => { Model.CleanDisplay(); await _manager.Search(Model); });
            FindFiles = ReactiveCommand.CreateFromTask(async() => { Model.CleanDisplay(); await _manager.FindFiles(Model); });
            Cancel    = ReactiveCommand.Create(() => _manager.Cancel());
            Copy      = ReactiveCommand.CreateFromTask(() => _manager.CopyToClipboard(Model.ResultDisplaySelectedItem));
        }