简体   繁体   English

在FileSystemWatcher检测到更改后读取文本文件的最后更改

[英]Reading the last changes of a text file after FileSystemWatcher detects change

Relating to my earlier post: Smartest way to monitor log-files in realtime? 与我之前的帖子有关: 实时监控日志文件的最智能方法?
I want a FileSystemWatcher to detect changes of a text file (logfile) and afterwards I want it to show me the latest changes! 我想要一个FileSystemWatcher来检测文本文件(日志文件)的变化,之后我想让它向我展示最新的变化!

They are big files >50bm, and a full scan almost every second is very time-consuming. 它们是大于50bm的大文件,几乎每秒都进行一次完整扫描非常耗时。
I though .last() could help, but this code only returns the last row: 我虽然.last()可以提供帮助,但此代码仅返回最后一行:

 File.ReadText(@"C:\Filename.txt").Last();

I need some code that can continue reading from the last spot and just determine and returns the changes. 我需要一些代码可以继续从最后一个位置读取,然后确定并返回更改。

如果您只是在文件末尾附加数据,则可以在内存中保存最后读取字节的位置,并在每次通知文件更新时从该位置读取到文件末尾。

FileSystemWatcher won't exactly do what you want. FileSystemWatcher不会完全按照你的意愿行事。 It sees changes at the directory level. 它会在目录级别看到更改。 Once the file changes it has no history of it's previous contents. 文件更改后,它没有以前内容的历史记录。 Calling Last() on File.ReadText() also has no concept of the files previous state which is why it just returns the last row. File.ReadText() Last()上调用Last()也没有文件先前状态的概念,这就是为什么它只返回最后一行。

I think the best approach would be to save the EOF's position then each time the FSW raises a file changed event you would read from previous EOF position to the new EOF then update the variable which stores the EOF's position. 我认为最好的方法是保存EOF的位置然后每次FSW将你从前一个EOF位置读取的文件改变事件提升到新的EOF然后更新存储EOF位置的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM