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

18

我需要一段能读取日志文件中最后n行的代码片段。我从网上找到了下面这段代码。由于日志文件可能非常大,因此我希望避免读取整个文件所带来的开销。有人可以建议性能优化吗?我不想逐个字符地读取文件并更改位置。

   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();

你不能这样使用StreamReader。 - SLaks
请查看https://dev59.com/4XM_5IYBdhLWcg3wslRW。然后,您可以在IEnumerable上使用LINQ扩展`.Last()`来获取最后N行。 - Russ Cam
@Russ: 不行,LINQ 无法高效地提供最后 n 行。 - SLaks
@Slaks - 哎呀!我以为有一个获取最后N个项目的重载...今天真是漫长的一天!现在我想想,这需要在结尾回溯一次才能获取N个项目。 - Russ Cam
1
https://dev59.com/YnRC5IYBdhLWcg3wJNgN - CodesInChaos
9个回答

10

你的代码性能会非常差,因为你没有允许缓存。此外,它将完全无法处理 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;
}

2
我真的很喜欢移动缓冲区的想法。但是这样做不会有效地读取整个日志文件吗?有没有一种有效的方法可以“寻找”到第n行的开头,然后从那里执行readLine()操作。这可能是我的一个愚蠢的疑问!! - frictionlesspulley
2
尝试访问 https://dev59.com/YnRC5IYBdhLWcg3wJNgN#398512 - SLaks

4

我的一个朋友使用了这种方法(可以在这里找到BackwardReader):

public static IList<string> GetLogTail(string logname, string numrows)
{
    int lineCnt = 1;
    List<string> lines = new List<string>();
    int maxLines;

    if (!int.TryParse(numrows, out maxLines))
    {
        maxLines = 100;
    }

    string logFile = HttpContext.Current.Server.MapPath("~/" + logname);

    BackwardReader br = new BackwardReader(logFile);
    while (!br.SOF)
    {
        string line = br.Readline();
        lines.Add(line + System.Environment.NewLine);
        if (lineCnt == maxLines) break;
        lineCnt++;
    }
    lines.Reverse();
    return lines;
}

4
为什么numrows是字符串? - SLaks
和SLaks一样的问题,但是因为BackwardReader加1。我不知道它。 - BrunoLM
说实话,SLaks,我在我朋友的博客文章中找不到任何解释为什么这样做的内容。我可以看出这本质上是从JavaScript调用的WCF方法,但我不确定是否足够解释它。 - Jesse C. Slicer
那个BackwardReader的实现很慢(因为它不缓冲)并且不能支持Unicode。 - SLaks
我刚刚看了一下博客,发现他使用了ASCIIEncoding,因此它不适用于Unicode或任何其他编码。 - phuclv
2
BackwardReader的链接不再可用。 - Maarten

4

如果您的代码遇到了问题,这是我的版本。由于它是日志文件,可能会有其他程序在写入,因此最好确保不要锁定它。

您可以直接跳到日志末尾,然后从末尾开始向前阅读,直到阅读到 n 行为止。然后从那里开始阅读所有内容。

        int n = 5; //or any arbitrary number
        int count = 0;
        string content;
        byte[] buffer = new byte[1];

        using (FileStream fs = new FileStream("text.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            // read to the end.
            fs.Seek(0, SeekOrigin.End);

            // read backwards 'n' lines
            while (count < n)
            {
                fs.Seek(-1, SeekOrigin.Current);
                fs.Read(buffer, 0, 1);
                if (buffer[0] == '\n')
                {
                    count++;
                }

                fs.Seek(-1, SeekOrigin.Current); // fs.Read(...) advances the position, so we need to go back again
            }
            fs.Seek(1, SeekOrigin.Current); // go past the last '\n'

            // read the last n lines
            using (StreamReader sr = new StreamReader(fs))
            {
                content = sr.ReadToEnd();
            }
        }

1
我喜欢这个解决方案,可以避免读取整个文件,但是我想添加检查 fs.Position > 0 应该包括在内,以避免超过开头位置进行搜索。 - stratocaster_master
这段代码运行良好,但是如果请求的行数大于文件中实际行数,它会出现 System.IO.IOException 异常。 - Sean N.

2
你的日志中的行是否长度相似?如果是,你可以计算出平均行长度,然后按照以下步骤操作:
  1. 将指针定位到文件结尾处 - 所需行数 * 平均行长度(之前的位置)
  2. 读取直到文件结尾处
  3. 如果已经取得了足够的行,那就没事了。否则,将指针定位到之前的位置 - 所需行数 * 平均行长度
  4. 读取直到之前的位置
  5. 回到第3步
内存映射文件也是一种不错的方法 -- 将文件尾部映射到内存,计算行数,映射之前的块,再次计算行数等等,直到获得所需行数。

这是一个很棒的答案,适用于只需要大约返回行数的情况。大大减少了循环次数和时间。将我的实现作为答案添加进去了。 - Ash

2

以下是我的回答:

    private string StatisticsFile = @"c:\yourfilename.txt";

    // Read last lines of a file....
    public IList<string> ReadLastLines(int nFromLine, int nNoLines, out bool bMore)
    {
        // Initialise more
        bMore = false;
        try
        {
            char[] buffer = null;
            //lock (strMessages)  Lock something if you need to....
            {
                if (File.Exists(StatisticsFile))
                {
                    // Open file
                    using (StreamReader sr = new StreamReader(StatisticsFile))
                    {
                        long FileLength = sr.BaseStream.Length;

                        int c, linescount = 0;
                        long pos = FileLength - 1;
                        long PreviousReturn = FileLength;
                        // Process file
                        while (pos >= 0 && linescount < nFromLine + nNoLines) // Until found correct place
                        {
                            // Read a character from the end
                            c = BufferedGetCharBackwards(sr, pos);
                            if (c == Convert.ToInt32('\n'))
                            {
                                // Found return character
                                if (++linescount == nFromLine)
                                    // Found last place
                                    PreviousReturn = pos + 1; // Read to here
                            }
                            // Previous char
                            pos--;
                        }
                        pos++;
                        // Create buffer
                        buffer = new char[PreviousReturn - pos];
                        sr.DiscardBufferedData();
                        // Read all our chars
                        sr.BaseStream.Seek(pos, SeekOrigin.Begin);
                        sr.Read(buffer, (int)0, (int)(PreviousReturn - pos));
                        sr.Close();
                        // Store if more lines available
                        if (pos > 0)
                            // Is there more?
                            bMore = true;
                    }
                    if (buffer != null)
                    {
                        // Get data
                        string strResult = new string(buffer);
                        strResult = strResult.Replace("\r", "");

                        // Store in List
                        List<string> strSort = new List<string>(strResult.Split('\n'));
                        // Reverse order
                        strSort.Reverse();

                        return strSort;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("ReadLastLines Exception:" + ex.ToString());
        }
        // Lets return a list with no entries
        return new List<string>();
    }

    const int CACHE_BUFFER_SIZE = 1024;
    private long ncachestartbuffer = -1;
    private char[] cachebuffer = null;
    // Cache the file....
    private int BufferedGetCharBackwards(StreamReader sr, long iPosFromBegin)
    {
        // Check for error
        if (iPosFromBegin < 0 || iPosFromBegin >= sr.BaseStream.Length)
            return -1;
        // See if we have the character already
        if (ncachestartbuffer >= 0 && ncachestartbuffer <= iPosFromBegin && ncachestartbuffer + cachebuffer.Length > iPosFromBegin)
        {
            return cachebuffer[iPosFromBegin - ncachestartbuffer];
        }
        // Load into cache
        ncachestartbuffer = (int)Math.Max(0, iPosFromBegin - CACHE_BUFFER_SIZE + 1);
        int nLength = (int)Math.Min(CACHE_BUFFER_SIZE, sr.BaseStream.Length - ncachestartbuffer);
        cachebuffer = new char[nLength];
        sr.DiscardBufferedData();
        sr.BaseStream.Seek(ncachestartbuffer, SeekOrigin.Begin);
        sr.Read(cachebuffer, (int)0, (int)nLength);

        return BufferedGetCharBackwards(sr, iPosFromBegin);
    }

注意:

  1. 调用ReadLastLines函数时,从0开始以nLineFrom作为最后一行,以nNoLines作为要读取的行数。
  2. 它会反转列表,因此第一个是文件中的最后一行。
  3. 如果还有更多行要读取,则bMore返回true。
  4. 它将数据缓存到1024个字符块中-因此速度很快,您可能需要增加此大小以处理非常大的文件。

享受吧!


2

这并不是最优解,但对于使用较小的日志文件进行快速检查时,我已经使用了类似下面这样的方法:

List<string> mostRecentLines = File.ReadLines(filePath)
    // .Where(....)
    // .Distinct()
    .Reverse()
    .Take(10)
    .ToList()

0

大多数日志文件都有 DateTime 印记。虽然可以改进,但是如果您想要最近 N 天的日志消息,则下面的代码运行良好。

    /// <summary>
    /// Returns list of entries from the last N days.
    /// </summary>
    /// <param name="N"></param>
    /// <param name="cSEP">field separator, default is TAB</param>
    /// <param name="indexOfDateColumn">default is 0; change if it is not the first item in each line</param>
    /// <param name="bFileHasHeaderRow"> if true, it will not include the header row</param>
    /// <returns></returns>
    public List<string> ReadMessagesFromLastNDays(int N, char cSEP ='\t', int indexOfDateColumn = 0, bool bFileHasHeaderRow = true)
    {
        List<string> listRet = new List<string>();

        //--- replace msFileName with the name (incl. path if appropriate)
        string[] lines = File.ReadAllLines(msFileName);

        if (lines.Length > 0)
        {
            DateTime dtm = DateTime.Now.AddDays(-N);

            string sCheckDate = GetTimeStamp(dtm);
            //--- process lines in reverse
            int iMin = bFileHasHeaderRow ? 1 : 0;
            for (int i = lines.Length - 1; i >= iMin; i--)  //skip the header in line 0, if any
            {
                if (lines[i].Length > 0)  //skip empty lines
                {
                    string[] s = lines[i].Split(cSEP);
                    //--- s[indexOfDateColumn] contains the DateTime stamp in the log file
                    if (string.Compare(s[indexOfDateColumn], sCheckDate) >= 0)
                    {
                        //--- insert at top of list or they'd be in reverse chronological order
                        listRet.Insert(0, s[1]);    
                    }
                    else
                    {
                        break; //out of loop
                    }
                }
            }
        }

        return listRet;
    }

    /// <summary>
    /// Returns DateTime Stamp as formatted in the log file
    /// </summary>
    /// <param name="dtm">DateTime value</param>
    /// <returns></returns>
    private string GetTimeStamp(DateTime dtm)
    {
        // adjust format string to match what you use
        return dtm.ToString("u");
    }

0
正如@EugeneMayevski在上面所述,如果您只需要返回大约的行数,每行的长度大致相同,并且您更关心大文件的性能,那么这是更好的实现方式:
    internal static StringBuilder ReadApproxLastNLines(string filePath, int approxLinesToRead, int approxLengthPerLine)
    {
        //If each line is more or less of the same length and you don't really care if you get back exactly the last n
        using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            var totalCharsToRead = approxLengthPerLine * approxLinesToRead;
            var buffer = new byte[1];
             //read approx chars to read backwards from end
            fs.Seek(totalCharsToRead > fs.Length ? -fs.Length : -totalCharsToRead, SeekOrigin.End);
            while (buffer[0] != '\n' && fs.Position > 0)                   //find new line char
            {
                fs.Read(buffer, 0, 1);
            }
            var returnStringBuilder = new StringBuilder();
            using (StreamReader sr = new StreamReader(fs))
            {
                returnStringBuilder.Append(sr.ReadToEnd());
            }
            return returnStringBuilder;
        }
    }

0
现在在C# 4.0中(以及在早期版本中只需花费一点点的努力),您可以非常轻松地使用内存映射文件来执行此类操作。它非常适用于大型文件,因为您可以仅映射文件的一部分,然后将其作为虚拟内存访问。
这里有一个很好的例子

这是一个好主意,但据我所知它不允许按行(文本)读取文件,就像问题所问的那样。 - AaA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接