C#:文本文件的尾部程序

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

问题描述

我有一个不断记录短行的日志文件.我需要开发一个服务来响应(或轮询或侦听)添加到该文件的新行,这是一种 unix 的尾部程序,以便我的服务始终保持最新状态以保护文件.

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 来通知您关注的一个或多个文件的更改.从那里,我将缓存事件之间的文件大小等信息,并添加一些逻辑以仅响应整行等.您可以使用 FileStream 的 Seek() 方法 类跳转到文件中的特定点并从那里只读.考虑到这些功能,如果您需要,手动推出此功能应该不会太难.

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#:文本文件的尾部程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:是否有像文件流一样阻塞的内存流 下一篇:将 short[] 转换为可以作为音频播放的 Stream

相关文章

最新文章