예제 #1
0
        private void OpenFiles(IEnumerable <string> filePaths, IFilter filter = null)
        {
            string extension = FileExtensionModule.GetFileExtension(filePaths);

            // The paths is not guaranteed to be files, considering other log sources.
            // so we only save general info here.
            var initializeString = string.Join(",", filePaths);

            if (Settings.Default.Data_RecentFiles.Contains(initializeString))
            {
                Settings.Default.Data_RecentFiles.Remove(initializeString);
            }

            Settings.Default.Data_RecentFiles.Insert(0, initializeString);
            if (Settings.Default.Data_RecentFiles.Count > Settings.Default.Data_MaxHistoryCount)
            {
                Settings.Default.Data_RecentFiles.RemoveAt(Settings.Default.Data_RecentFiles.Count - 1);
            }

            Settings.Default.Save();

            var logSource = LogSourceManager.Instance.GetLogSource(initializeString, new LogSourceProperties(
                                                                       Settings.Default.Behavior_AutoLoad,
                                                                       Settings.Default.Behavior_EnabledCompression),
                                                                   extension);

            var document = new RootView <DataItemBase>(logSource, filter);

            this.AddView(document);
        }
예제 #2
0
        private async void buttonSearch_Click(object sender, EventArgs e)
        {
            try
            {
                this.cts?.Cancel();

                this.cts?.Dispose();
                this.EnableButtons(false);

                this.currentFinished    = 0;
                this.progressBar1.Value = 0;


                this.cts = new CancellationTokenSource();

                if (string.IsNullOrEmpty(this.comboBoxSearchPattern.Text))
                {
                    this.Filter = LogFlow.DataModel.Filter.MatchAll;
                }
                else
                {
                    this.Filter = LogFilterInterpreter.Parse(this.comboBoxSearchPattern.Text);
                }

                var filePaths = Directory.EnumerateFiles(
                    this.comboBoxSearchFolder.Text,
                    this.comboBoxFileNamePattern.Text,
                    this.checkBoxRecursive.Checked ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
                                .ToList();

                string extension = FileExtensionModule.GetFileExtension(filePaths);

                this.totalCount = filePaths.Count;

                this.dataGridViewResult.Rows.Clear();
                this.dataGridViewResult.Rows.AddRange(filePaths.Select(p => new DataGridViewRow()
                {
                    Cells =
                    {
                        new DataGridViewTextBoxCell()
                        {
                            Value = Path.GetFileName(p)
                        },
                        new DataGridViewTextBoxCell()
                        {
                            Value = string.Empty
                        },
                        new DataGridViewTextBoxCell()
                        {
                            Value = "0 %"
                        },
                    },
                    Tag = p,
                }).ToArray());

                int lineCount;
                int.TryParse(this.comboBoxLineCount.Text, out lineCount);

                var token = this.cts.Token;

                await Task.WhenAll(Enumerable.Range(0, filePaths.Count).Select(i => Task.Run(() =>
                {
                    DataItemBase dataItem;

                    var logSource = LogSourceManager.Instance.GetLogSource(filePaths[i], new LogSourceProperties(
                                                                               false,
                                                                               true),
                                                                           extension);

                    try
                    {
                        foreach (var progress in logSource.Peek(this.Filter, lineCount, token))
                        {
                            if (token.IsCancellationRequested)
                            {
                                return;
                            }
                            this.dataGridViewResult.Rows[i].Cells[2].Value = $"{progress} %";
                        }
                        if (string.IsNullOrEmpty(this.dataGridViewResult.Rows[i].Cells[1].Value as string) && logSource.Count > 0)
                        {
                            dataItem = logSource[0];
                            this.dataGridViewResult.Rows[i].Cells[1].Value = string.Format(
                                logSource.Templates[dataItem.TemplateId],
                                dataItem.Parameters.Cast <object>().ToArray());
                        }
                        if (token.IsCancellationRequested)
                        {
                            return;
                        }
                        this.dataGridViewResult.Rows[i].Cells[2].Value = "100 %";

                        this.currentFinished++;
                    }
                    finally
                    {
                        (logSource as IDisposable)?.Dispose();

                        if (!token.IsCancellationRequested)
                        {
                            this.progressBar1.Invoke(
                                new Action(() => this.progressBar1.Value = 100 * this.currentFinished / this.totalCount));
                        }
                    }
                }, token)));

                if (token.IsCancellationRequested)
                {
                    return;
                }
                this.progressBar1.Value = 100;
                this.EnableButtons(true);

                // add to history
                Settings.Default.Data_RecentDirectories.Clear();
                Settings.Default.Data_RecentDirectories.AddRange(this.AdjustHistoryOfComboBox(this.comboBoxSearchFolder));
                Settings.Default.Data_FileNamePatterns.Clear();
                Settings.Default.Data_FileNamePatterns.AddRange(
                    this.AdjustHistoryOfComboBox(this.comboBoxFileNamePattern));

                Settings.Default.Data_SearchRecursive = this.checkBoxRecursive.Checked;

                Settings.Default.Save();
            }
            catch (ObjectDisposedException ex1)
            {
                Debug.WriteLine($"ObjectDisposedException in Search_Click: {ex1}");
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format(Resources.SearchFailedText, ex), Resources.SomethingWrong, MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }