protected virtual void OnChanged(object source, FileSystemEventArgs e) { try { if (this.includeSubdirectories && this.ShouldSkip(e.FullPath)) { return; } string relativeFilePath = e.Name; //Sometimes we receive event where e.name is null so we should just skip it if (string.IsNullOrEmpty(relativeFilePath) || ShouldExclude(relativeFilePath) || !ShouldInclude(relativeFilePath)) { return; } //The entries in _buffer should be deleted before _logfiles and added after _logfiles switch (e.ChangeType) { case WatcherChangeTypes.Deleted: if (!File.Exists(e.FullPath)) //macOS sometimes fires this event when a file is created so we need this extra check. { RemoveFromBuffer(relativeFilePath); _logFiles.Remove(relativeFilePath); BookmarkManager.RemoveBookmark(this.GetBookmarkName(e.FullPath)); } break; case WatcherChangeTypes.Created: if (!_logFiles.ContainsKey(relativeFilePath)) { _logFiles[relativeFilePath] = CreateLogSourceInfo(e.FullPath, 0); if (this.bookmarkOnBufferFlush) { BookmarkManager.RegisterBookmark(this.GetBookmarkName(e.FullPath), 0, (pos) => this.SaveBookmark()); } AddToBuffer(relativeFilePath); } break; case WatcherChangeTypes.Changed: AddToBuffer(relativeFilePath); break; } _logger?.LogDebug($"ThreadId{Thread.CurrentThread.ManagedThreadId} File: {e.FullPath} ChangeType: {e.ChangeType}"); } catch (Exception ex) { _logger?.LogError(ex.ToMinimized()); } }
protected virtual void OnRenamed(object source, RenamedEventArgs e) { try { // this is for Subdirectories check only if (this.includeSubdirectories && this.ShouldSkip(e.FullPath)) { return; } //Sometimes we receive event where e.name is null so we should just skip it if (string.IsNullOrEmpty(e.Name) || string.IsNullOrEmpty(e.OldName) || ShouldExclude(e.Name) || ShouldExclude(e.OldName) || (!ShouldInclude(e.Name) && !ShouldInclude(e.OldName))) { return; } //File name rotation RemoveFromBuffer(e.OldName); if (_logFiles.ContainsKey(e.OldName)) { var newSourceInfo = CreateLogSourceInfo(e.FullPath, _logFiles[e.OldName].Position); newSourceInfo.LineNumber = _logFiles[e.OldName].LineNumber; _logFiles[e.Name] = newSourceInfo; _logFiles.Remove(e.OldName); var bookmark = BookmarkManager.GetBookmark(this.GetBookmarkName(e.OldFullPath)); if (bookmark != null) { BookmarkManager.RemoveBookmark(bookmark.Id); BookmarkManager.RegisterBookmark(this.GetBookmarkName(e.FullPath), bookmark.Position, (id) => this.SaveBookmark()); } } else { var newSource = CreateLogSourceInfo(e.FullPath, 0); _logFiles.Add(e.Name, newSource); BookmarkManager.RegisterBookmark(this.GetBookmarkName(e.FullPath), 0, (id) => this.SaveBookmark()); } } catch (Exception ex) { _logger?.LogError(ex.ToMinimized()); } finally { AddToBuffer(e.Name); _logger?.LogInformation("File: {0} renamed to {1}", e.OldFullPath, e.FullPath); } }