问题描述
需要code片断这将读出日志文件的最后一个n行。我想出了从net.I以下code我还挺新的C锋利。由于日志文件可能是蛮大的,我想避免的读取整个file.Can有人提出任何性能增强开销。 我真的不想要阅读的每个字符并改变位置。
VAR读卡器=新的StreamReader(文件路径,Encoding.ASCII);
reader.BaseStream.Seek(0,SeekOrigin.End);
变种数= 0;
而(计数< = tailCount)
{
如果(reader.BaseStream.Position< = 0)中断;
reader.BaseStream.Position--;
INT C = reader.Read();
如果(reader.BaseStream.Position< = 0)中断;
reader.BaseStream.Position--;
如果(C =='\ N')
{
++计数;
}
}
VAR海峡= reader.ReadToEnd();
您code会执行得非常糟糕,因为你没有允许任何缓存的情况发生。
此外,它不会工作的所有的为统一code。
我写了下面执行:
///<总结>返回文本阅读器的结束和LT; /总结>
///< PARAM NAME =读者>读者从阅读< /参数>
///< PARAM NAME =lineCount>行返回的数量和LT; /参数>
///<返回>在最后lneCount线从阅读器< /回报>
公共静态字符串[]尾(这TextReader的读者,INT lineCount){
VAR缓冲区=新的名单,其中,串>(lineCount);
串线;
的for(int i = 0; I< lineCount;我++){
行= reader.ReadLine();
如果(线== NULL)返回buffer.ToArray();
buffer.Add(线);
}
INT lastLine所= lineCount - 1; //从缓冲器中读取的最后一行的索引。一切>该指数早于一切都被读取< =这个东印度
而(空!=(行= reader.ReadLine())){
lastLine所++;
如果(lastLine所== lineCount)lastLine所= 0;
缓冲区[lastLine所] =行;
}
如果(lastLine所== lineCount - 1)返回buffer.ToArray();
VAR retVal的=新的字符串[lineCount]
buffer.CopyTo(lastLine所+ 1,retVal的,0,lineCount - lastLine所 - 1);
buffer.CopyTo(0,retVal的,lineCount - lastLine所 - 1,lastLine所+ 1);
返回retVal的;
}
need a snippet of code which would read out last "n lines" of a log file. I came up with the following code from the net.I am kinda new to C sharp. Since the log file might bequite large, I want to avoid overhead of reading the entire file.Can someone suggest any performance enhancement. I do not really want to read each character and change position.
var reader = new StreamReader(filePath, Encoding.ASCII);
reader.BaseStream.Seek(0, SeekOrigin.End);
var count = 0;
while (count <= tailCount)
{
if (reader.BaseStream.Position <= 0) break;
reader.BaseStream.Position--;
int c = reader.Read();
if (reader.BaseStream.Position <= 0) break;
reader.BaseStream.Position--;
if (c == '\n')
{
++count;
}
}
var str = reader.ReadToEnd();
Your code will perform very poorly, since you aren't allowing any caching to happen.
In addition, it will not work at all for Unicode.
I wrote the following implementation:
///<summary>Returns the end of a text reader.</summary>
///<param name="reader">The reader to read from.</param>
///<param name="lineCount">The number of lines to return.</param>
///<returns>The last lneCount lines from the reader.</returns>
public static string[] Tail(this TextReader reader, int lineCount) {
var buffer = new List<string>(lineCount);
string line;
for (int i = 0; i < lineCount; i++) {
line = reader.ReadLine();
if (line == null) return buffer.ToArray();
buffer.Add(line);
}
int lastLine = lineCount - 1; //The index of the last line read from the buffer. Everything > this index was read earlier than everything <= this indes
while (null != (line = reader.ReadLine())) {
lastLine++;
if (lastLine == lineCount) lastLine = 0;
buffer[lastLine] = line;
}
if (lastLine == lineCount - 1) return buffer.ToArray();
var retVal = new string[lineCount];
buffer.CopyTo(lastLine + 1, retVal, 0, lineCount - lastLine - 1);
buffer.CopyTo(0, retVal, lineCount - lastLine - 1, lastLine + 1);
return retVal;
}
这篇关于如何阅读最后&QUOT; N&QUOT;日志文件的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!