在日志文件中查找总计行数? [英] Find the Total Lines in a log file?

查看:237
本文介绍了在日志文件中查找总计行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想知道在日志文件中确定多少行是最好的和最快的方式




目前我只是在做一个StreamReader.ReadLine并递增

a计数器直到我到达终点。还有更好的方法吗?


干杯,

Craig

Hi,

I was wondering the best and fastest way to determine how many lines are
in a log file.

At the moment I am simply doing a StreamReader.ReadLine and incrementing
a counter until I reach the end. Is there a better way??

Cheers,
Craig

推荐答案

这一切都取决于日志文件的大小。更大的日志

文件的常用方法

是采用统计抽样并获得平均线长然后使用

文件大小

来计算行数。这绝不是100%精确,但一般来说确实很好。如果每个日志行总是长度相同,那么你就是

真正的好运

从那以后操作非常简单。


至于使用StreamReader.ReadLine,这并不是很快。您正在为每行读取创建一个

字符串

对象。您可以通过使用ReadBytes并自行循环访问数据来检查用于行

终止

的字符(不是那个

很难),但是

你必须考虑具有不同线路终端标准的文件

(crlf vs cr vs lf),

因为各种系统都有自己的方法。如果这是你的日志文件,

那么你可以只用

搜索你写的任何东西。


-

Justin Rogers

DigiTec Web Consultants,LLC。

博客: http://weblogs.asp.net/justin_rogers


新闻:Oi ************** @ tk2msftngp13.phx.gbl ...
This all depends on how big the log file is. A common method for larger log
files
is to take a statistical sampling and gain an average line length then use the
file size
to compute the number of lines. This is never 100% precise, but does generally
work nicely. If each log line is always of the same length, then you are in
real luck
since then the operation is extremely easy.

As for using StreamReader.ReadLine, that isn''t exacty fast. You are creating a
string
object for each line read. You can check for the characters used for line
termination
yourself by using ReadBytes and cycling through the data yourself (not that
hard), but
you''ll have to take into account files with different line termination standards
(crlf vs cr vs lf),
since various systems all have their own methods. If this is your log file,
then you can just
search for whatever you''ve been writing out.

--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Craig Bumpstead" <cb******@yahoo.com.au> wrote in message
news:Oi**************@tk2msftngp13.phx.gbl...

我想知道确定日志文件中有多少行的最佳和最快方法。

目前我只是在做一个StreamReader.ReadLine并递增<直到我到达终点为止。还有更好的方法吗?

干杯,
Craig
Hi,

I was wondering the best and fastest way to determine how many lines are
in a log file.

At the moment I am simply doing a StreamReader.ReadLine and incrementing
a counter until I reach the end. Is there a better way??

Cheers,
Craig



Craig Bumpstead< cb **** **@yahoo.com.au>在新闻中写道:OixsJ
Craig Bumpstead <cb******@yahoo.com.au> wrote in news:OixsJ


FJEHA.3596

@ tk2msftngp13.phx.gbl:
FJEHA.3596
@tk2msftngp13.phx.gbl:


我想知道确定日志文件中有多少行的最佳和最快的方法。

目前我只是在做StreamReader.ReadLine和递增
一个柜台,直到我到达终点。有没有更好的方法??
Hi,

I was wondering the best and fastest way to determine how many lines are
in a log file.

At the moment I am simply doing a StreamReader.ReadLine and incrementing
a counter until I reach the end. Is there a better way??




如果你定义一行字符串以

Environment.NewLine结尾,你就可以这样将文件加载到缓冲区并计算

量的Environment.NewLine字符(如果

文件中的最后一个字符不是Environment.NewLine,则为+1。 )


FB


-

获取LLBLGen Pro,.NET的新O / R映射器: http://www.llblgen.com

我的.NET博客: http://weblogs.asp.net/fbouma

Microsoft C#MVP



If you define a line a string of characters ended with a
Environment.NewLine, you can thus load the file into a buffer and count the
amounts of Environment.NewLine characters (+1 if the last character in the
file isn''t an Environment.NewLine.)

FB

--
Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP


这篇关于在日志文件中查找总计行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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