public void Dispose() { Reset(); if (_threadPool != null) { _threadPool.Dispose(); _threadPool = null; } CloseFile(false); }
public void Dispose() { FileReloadedEvent = null; if (_threadPool != null) { _threadPool.Dispose(); _threadPool = null; } CloseFile(false); }
public LogFileStream(string configPath, string filePath, Encoding fileEncoding, int fileCheckFrequency, bool fileCheckPattern) { _fileEncoding = fileEncoding; _filePath = filePath; _filePathAbsolute = Path.Combine(configPath, _filePath); if (fileCheckFrequency > 0) _fileCheckFrequency = TimeSpan.FromSeconds(fileCheckFrequency); _fileCheckPattern = fileCheckPattern; if (_fileCheckPattern) _threadPool = new ThreadPoolQueue(); LoadFile(_filePathAbsolute, _fileEncoding, _fileCheckPattern); }
public LogFileStream(string configPath, string filePath, Encoding fileEncoding, int fileCheckFrequency, bool fileCheckPattern) { _fileEncoding = fileEncoding; _filePath = filePath; _filePathAbsolute = Path.Combine(configPath, _filePath); if (fileCheckFrequency > 0) { _fileCheckFrequency = TimeSpan.FromSeconds(fileCheckFrequency); } _fileCheckPattern = fileCheckPattern; if (_fileCheckPattern) { _threadPool = new ThreadPoolQueue(); } LoadFile(_filePathAbsolute, _fileEncoding, _fileCheckPattern); }
public void LoadConfig(TailFileConfig tailConfig, string configPath) { _configPath = configPath; try { new DirectoryInfo(Path.GetDirectoryName(Path.Combine(configPath, tailConfig.FilePath))); } catch (System.ArgumentException ex) { MessageBox.Show(this, String.Format("Failed to open file:\n\n{0}\n\nError:{1}", tailConfig.FilePath, ex.Message), "Invalid filename", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); Close(); return; } if (tailConfig.FileCacheSize <= 0) tailConfig.FileCacheSize = 1000; if (tailConfig.FormBackColor != null) _tailListView.BackColor = tailConfig.FormBackColor.Value; if (tailConfig.FormTextColor != null) _tailListView.ForeColor = tailConfig.FormTextColor.Value; if (tailConfig.FormFont != null) _tailListView.Font = tailConfig.FormFont; if (tailConfig.FormBookmarkBackColor != null) _bookmarkBackColor = tailConfig.FormBookmarkBackColor.Value; if (tailConfig.FormBookmarkTextColor != null) _bookmarkTextColor = tailConfig.FormBookmarkTextColor.Value; if (tailConfig.FileChangeCheckInterval > 0) _tailTimer.Interval = tailConfig.FileChangeCheckInterval; _externalTools = tailConfig.ExternalTools; externalToolsToolStripMenuItem.DropDownItems.Clear(); externalToolsToolStripMenuItem.Enabled = false; if (_externalTools != null) { foreach (ExternalToolConfig externalTool in _externalTools) { ToolStripMenuItem toolItem = externalToolsToolStripMenuItem.DropDownItems.Add(externalTool.Name) as ToolStripMenuItem; if (toolItem != null) { toolItem.Tag = externalTool; toolItem.Click += new EventHandler(externalToolMenuItem_Click); if (externalTool.ShortcutKeyEnum.HasValue) toolItem.ShortcutKeys = externalTool.ShortcutKeyEnum.Value; } externalToolsToolStripMenuItem.Enabled = true; } } _loghitCounter = -1; _keywordHighlight = tailConfig.KeywordHighlight; if (_keywordHighlight != null) { foreach (TailKeywordConfig keyword in _keywordHighlight) { if (keyword.MatchRegularExpression) { if (keyword.MatchCaseSensitive) keyword.KeywordRegex = new System.Text.RegularExpressions.Regex(keyword.Keyword); else keyword.KeywordRegex = new System.Text.RegularExpressions.Regex(keyword.Keyword, System.Text.RegularExpressions.RegexOptions.IgnoreCase); } else keyword.KeywordRegex = null; if (keyword.LogHitCounter) _loghitCounter = 0; if (!string.IsNullOrEmpty(keyword.ExternalToolName)) { keyword.ExternalToolConfig = _externalTools.Find((externalTool) => string.Compare(externalTool.Name, keyword.ExternalToolName) == 0); if (_threadPoolQueue == null) _threadPoolQueue = new ThreadPoolQueue(); // Prepare the threadpool for use } } } Encoding fileEncoding = tailConfig.EnumFileEncoding; if (_logTailStream != null) _logTailStream.Reset(); if (_logFileStream == null || _logFileStream.FilePath != tailConfig.FilePath || _logFileStream.FileEncoding != fileEncoding || _logFileStream.FileCheckInterval != tailConfig.FileCheckInterval || _logFileStream.FileCheckPattern != tailConfig.FileCheckPattern) { if (_logFileStream != null) _logFileStream.Dispose(); _logFileStream = new LogFileStream(configPath, tailConfig.FilePath, fileEncoding, tailConfig.FileCheckInterval, tailConfig.FileCheckPattern); } if (_logTailStream == null || _logTailStream.FilePath != tailConfig.FilePath || _logTailStream.FileEncoding != fileEncoding || _logTailStream.FileCheckInterval != tailConfig.FileCheckInterval || _logTailStream.FileCheckPattern != tailConfig.FileCheckPattern) { if (_logTailStream != null) _logTailStream.Dispose(); _logTailStream = new LogFileStream(configPath, tailConfig.FilePath, fileEncoding, tailConfig.FileCheckInterval, tailConfig.FileCheckPattern); if (_logTailStream.Length > 500 * 1024 * 1024) { if (MessageBox.Show(this, String.Format("The file is very large, sure you want to open it?\n\nFile Name: {0}\nFile Size: {1} Megabytes", _logTailStream.FilePath, _logTailStream.Length / 1024 / 1024), "Large file detected", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No) { Close(); return; } } if (_logFileCache != null) { _logFileCache.Reset(); _logFileCache = null; // Reset Cache, as the file contents can have changed } } _logTailStream.FileReloadedEvent += new EventHandler(_logTailStream_FileReloadedEvent); if (_logFileCache != null) _logFileCache.Reset(); if (_logFileCache == null || _logFileCache.Items.Count != tailConfig.FileCacheSize) { _logFileCache = new LogFileCache(tailConfig.FileCacheSize); _logFileCache.LoadingFileEvent += new EventHandler(_logFileCache_LoadingFileEvent); _logFileCache.FillCacheEvent += new EventHandler(_logFileCache_FillCacheEvent); // Add loading of cache while counting lines in file int lineCount = _logFileCache.FillTailCache(_logTailStream); _tailListView.VirtualListSize = lineCount; } else { _logFileCache.LoadingFileEvent += new EventHandler(_logFileCache_LoadingFileEvent); _logFileCache.FillCacheEvent += new EventHandler(_logFileCache_FillCacheEvent); } if (_taskMonitor != null) { _taskMonitor.Dispose(); _taskMonitor = null; } if (!string.IsNullOrEmpty(tailConfig.ServiceName)) _taskMonitor = new TaskMonitor(tailConfig.ServiceName); _formTitleMatchFilename = tailConfig.TitleMatchFilename; if (_formTitleMatchFilename) _formTitle = Path.GetFileName(_logTailStream.Name); else if (tailConfig.Title != null) _formTitle = tailConfig.Title; else _formTitle = Path.GetFileName(tailConfig.FilePath); UpdateFormTitle(true); _displayTabIcon = tailConfig.DisplayTabIcon; if (!string.IsNullOrEmpty(tailConfig.IconFile)) { _formIconFile = tailConfig.IconFile; string formIconFilePathAbsolute = System.IO.Path.Combine(configPath, _formIconFile); try { _formCustomIcon = System.Drawing.Icon.ExtractAssociatedIcon(formIconFilePathAbsolute); _formMaximizedIcon = Icon; Icon = _formCustomIcon; } catch (Exception ex) { MessageBox.Show(this, "Failed to load icon\n\n " + formIconFilePathAbsolute + "\n\n" + ex.Message, null, MessageBoxButtons.OK, MessageBoxIcon.Error); } } UpdateFormTitle(true); if (Visible) { if (_tailListView.VirtualListSize > 0) { _tailListView.EnsureVisible(_tailListView.VirtualListSize - 1); _tailListView.FocusedItem = _tailListView.Items[_tailListView.VirtualListSize - 1]; _tailListView.SelectedIndices.Add(_tailListView.VirtualListSize - 1); _tailListView.Invalidate(); _tailListView.Update(); } } }