如何读取日志文件的最后"n"行

fri*_*ley 16 .net c# performance file-io

需要一段代码来读出日志文件的最后"n行".我从网上得到了以下代码.我是C sharp的新手.由于日志文件可能非常大,我想避免读取整个文件的开销.有人建议任何性能增强.我真的不想读每个角色并改变位置.

   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();
Run Code Online (Sandbox Code Playgroud)

SLa*_*aks 9

您的代码执行效果非常差,因为您不允许任何缓存发生.
此外,它不会工作在所有的Unicode.

我写了以下实现:

///<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;
}
Run Code Online (Sandbox Code Playgroud)

  • 真的很喜欢换档缓冲区的想法.但是这不会有效地读取整个日志文件.有没有一种有效的方法来"寻找"到第n行的开头.并从那里做一个readLine().这可能是我的一个愚蠢的怀疑! (2认同)
  • @frictionlesspulley:试试http://stackoverflow.com/questions/398378/get-last-10-lines-of-very-large-text-file-10gb-c/398512#398512 (2认同)