C#:尾巴象程序的文本文件 [英] C#: tail like program for text file

查看:183
本文介绍了C#:尾巴象程序的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不断地记录短行的日志文件。我需要开发出反应的服务(或投票,或听),以添加到该文件中,一种UNIX'尾巴软件的线条,让我的服务始终是最新的reguarding文件。

I have a log file that continually logs short lines. I need to develop a service that reacts (or polls, or listens to) to new lines added to that file, a sort of unix' tail program, so that my service is always up to date reguarding the file.

我不认为打开一个读数据流,并保持它打开是个好主意。也许我应该使用FileSystemWatcher类。

I don't think that opening a read stream and keeping it opened is a good idea. Maybe I should use the FileSystemWatcher class.

长话短说,我需要实时的每一个新的行添加到该文件进行解析。

Long story short, I need to parse in real time every new line added to this file.

任何想法,帮助或指示非常感谢。

Any idea help or indication is really appreciated.

修改

由于我一直不是很清楚。我不需要任何程序,我的的程序。对于阅读(再处理),每一个新的行添加到文件中。我的意思是什么,我寻找的是一个方法(或:如何实现这一点?)不断为拖尾的,保持在一个文件被写入

As I've been not very clear. I do not need any program, I am writing a program. For reading (then processing) every new line added to the file. I mean that what I'm looking for is a methodology (or: how to implement this?) for continually tailing a file that keeps on been written.

我要开发一个Windows服务侦听这一文件并执行对每个新线运营。

I have to develop a Windows service that "listens" to this file and does operations on every new line.

因此,如果在给定时刻的文件是:

So, if in a given moment the file is:

12.31.07 - jdoe [log on] 347
12.32.08 - ssmith [log on] 479
12.32.08 - mpeterson [log off] 532
12.32.09 - apacino [log on] 123

在那一刻行

12.32.11 - pchorr [log on] 127

被添加到日志程序日志文件(我没有访问),我需要我的Windows服务作出反应到行瘾,拦截新行( 12.32.11 - pchorr [登录] 127 ),并对其进行处理。等等。

is added to the log file by the logging program (that I have not access to), I need my Windows service to "react" to the line addiction, intercept the new line (12.32.11 - pchorr [log on] 127) and process it. And so on.

现在,我不知道如何做到这一点。我应查询该文件每n秒,最后读线在内存中存储和处理只新添加的行。这个问题是很慢,再加上我会阅读每一次非常大的文件。

Now, I don't know how to do this. I should poll the file every n seconds, store the last read line in memory and process only the newly added lines. The problem with this is that is very slow, plus I'd be reading a very large file every time.

或者,也许我可以用 FileSystemWatcher的,但我还没有发现使用它类似用途的例子。

Or maybe I could use FileSystemWatcher, but I haven't found any example of using it for similar purposes.

所以,你会建议完成工作?谢谢你。

So, what would you suggest to get the work done? Thanks.

推荐答案

我会建议使用 FileSystemWatcher的将变更通知该文件或文件你担心。从那里,我会缓存信息,如事件之间的文件的大小,并添加一些逻辑来只响应饱满的线条等。您可以使用 SEEK()方法在的FileStream的类跳转到文件中的特定点,只有从那里读取。鉴于这些特点,应该不会太难手工卷本的功能,如果这就是你所需要的。

I would recommend using FileSystemWatcher to be notified of changes to the file or files you're concerned about. From there, I would cache information such as the size of the file between events and add some logic to only respond to full lines, etc. You can use the Seek() method of the FileStream class to jump to a particular point in the file and read only from there. Given these features, it shouldn't be too hard to hand-roll this functionality if that's what you need.

这篇关于C#:尾巴象程序的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆