监视文件的更改
我一直在网上搜索有关创建应用程序的好主意。服务或 vb 脚本将持续监视文件的更改。但是,我希望它在没有变化时提醒我。我正在监视的文件是由一个非常重要的应用程序创建的日志。如果该文件停止增长,则意味着应用程序出现问题,并且已停止写入日志文件。
有人有什么想法吗?我已经尝试过 C# 中的 FileSystemWatcher 类,但我不知道是否有一种方法可以在文件未更改时发出警报,因为所有事件都会查看更改。
谢谢。
I have been searching the web for good ideas on creating an application. service, or vb script that will constantly monitor a file for changes. However, I want it to alert me when there are NO changes. The file I am monitoring is a log created by a very important application. If that file stops growing then that means there is something wrong with the application and it has stopped writing to the log file.
Does anyone have any ideas? I have played around with the FileSystemWatcher class in C# but I don't know if there is a way to use to alert when a file does not change since all of the events look at changes.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需使用一个计时器,该计时器按该文件应更新的时间间隔触发。
检查文件大小(
FileInfo.Length
),如果它与上次计时器触发时相同,则它不会增长。Just use a timer that fires on an interval that that file should be updated in.
Check the file size (
FileInfo.Length
) and if it is the same as the last time the timer fired, it is not growing.在这种情况下,您不需要观察者,您可以使用计时器。每隔几分钟(或任何适合您需要的时间)检查文件大小。将最后的尺寸存储在某处。下次检查时,将前一个值与当前值进行比较。如果它们相同,则文件没有更改,您可以发出警报。
In this case you don't need a watcher, you could use a timer. Every few minutes (or whatever fits your needs), check the file size. Store the last size somewhere. The next time you check it, compare the previous value to the current one. If they are the same then the file has not changed and you can issue your alert.
文件大多数时候不会改变。您需要定义一个时间段,超过该时间段记录过程将被视为已停止。启动一个计时器(当它完成时)将发出日志记录已停止的信号。每次发生更改事件时都会重置计时器。
A file doesn't change most of the time. You need to define a time period beyond which the logging process is considered to have stopped. Start a timer that (when it completes) will signal that logging has stopped. Reset the timer every time a change event rolls in.
您可以使用
FileSytemWatcher
类:http://msdn.microsoft.com/en-us/library /system.io.filesystemwatcher.aspx
更新:嗯,从头到尾阅读问题是值得的...您可以设置一个
计时器
(ref) 用于您希望收到警报的不活动持续时间并在 FileSystemWatcher 事件中重置计时器。You can use the
FileSytemWatcher
class:http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
update: well, it pays to read the question through to the end... You can set a
Timer
(ref) for the duration of inactivity you want to be alerted of and reset the timer inFileSystemWatcher
events.我建议你结合你已经得到的所有答案。
使用 FileSytemWatcher 类监视更改并使用计时器等待自上次更改以来的一段时间。
最简单的解决方案将类似于 ManualResetEvent 和 DateTime 变量(不要忘记内存屏障)和后台线程。
所以,你会得到这样的东西:
I suggest you to combine all answers you already got.
Use FileSytemWatcher class to watch for changes and use a timer to wait for some period of time since last change.
The simplest solution will look like ManualResetEvent and DateTime variables (don't forget memory barriers) and a background thread.
So, you'll have a something like this: