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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1197226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:38:32  来源:igfitidea点击:

C#: tail like program for text file

c#loggingfilesystemsstream

提问by pistacchio

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.

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

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

我不认为打开读取流并保持打开状态是一个好主意。也许我应该使用 FileSystemWatcher 类。

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.

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

EDIT

编辑

As I've been not very clear. I do not need any program, I am writinga 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 tailinga file that keeps on been written.

因为我一直不是很清楚。我不需要任何程序,我正在编写程序。用于读取(然后处理)添加到文件中的每个新行。我的意思是什么,我寻找的是一个方法(或:如何实现这一点?)用于持续拖尾上被写入保留的文件。

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

我必须开发一个 Windows 服务来“监听”这个文件并在每个新行上执行操作。

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

in the very moment that the line

就在这条线的那一刻

12.32.11 - pchorr [log on] 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.

由日志程序(我无法访问)添加到日志文件中,我需要我的 Windows 服务对行瘾“做出反应”,拦截新行 ( 12.32.11 - pchorr [log on] 127) 并对其进行处理。等等。

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.

现在,我不知道该怎么做。我应该每 n 秒轮询一次文件,将最后读取的行存储在内存中并仅处理新添加的行。这样做的问题是速度很慢,而且我每次都会读取一个非常大的文件。

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

或者也许我可以使用FileSystemWatcher,但我还没有找到将它用于类似目的的任何示例。

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

那么,你有什么建议来完成这项工作?谢谢。

采纳答案by Lee

I would recommend using FileSystemWatcherto 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 FileStreamclass 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.

我建议使用FileSystemWatcher通知您关注的一个或多个文件的更改。从那里,我将缓存事件之间的文件大小等信息,并添加一些逻辑以仅响应整行等。您可以使用类的Seek()方法FileStream跳转到文件中的特定点并只从那里。鉴于这些功能,如果您需要手动滚动此功能应该不会太难。

回答by Giorgio Galante

You haven't really explained whether you need a tail-like program for Windows i.e. http://www.baremetalsoft.com/baretail/or if you want a windows version of tail (use cygwin) or if you're looking for some sort of log monitoring API....

你还没有真正解释你是否需要一个类似 tail 的 Windows 程序,即 http://www.baremetalsoft.com/baretail/或者你是否想要一个 windows 版本的 tail(使用 cygwin)或者如果你正在寻找一些某种日志监控API ....

回答by madhu

Simple solution would be use , sample code provided in http://www.codeproject.com/Articles/7568/Tail-NETarticle. It is just one function copy/paste into your code.

简单的解决方案是使用http://www.codeproject.com/Articles/7568/Tail-NET文章中提供的示例代码。它只是将一个函数复制/粘贴到您的代码中。

回答by Doc

It is important to note that Microsoft (since vista/svr08) no longer updates file metadata when a file is updated (such as a log file being updated by a service).

需要注意的是,Microsoft(自 vista/svr08 起)不再在文件更新时(例如服务更新日志文件)更新文件元数据。

For example, the metadata for a file such as modified date, will not be updateduntil the file is closedby the service/program which is updating the log file.

例如,在更新日志文件的服务/程序关闭文件之前,文件的元数据(例如修改日期)不会更新

Therefore FileSystemWatcherwill NOT catch log file updates as you might expect.

因此FileSystemWatcher不会像您期望的那样捕获日志文件更新。

https://blogs.technet.microsoft.com/asiasupp/2010/12/14/file-date-modified-property-are-not-updating-while-modifying-a-file-without-closing-it/

https://blogs.technet.microsoft.com/asiasupp/2010/12/14/file-date-modified-property-are-not-updating-while-modifying-a-file-without-closure-it/