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

时间:2011-01-06 20:53:04

标签: c# .net 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();

9 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:5)

我的一位朋友使用this methodBackwardReader可以找到here):

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;
}

答案 2 :(得分: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. 使用nLineFrom调用ReadLastLines,从最后一行开始为0,将nNoLines作为要读回的行数。
  2. 它会反转列表,因此第一行是文件中的最后一行。
  3. 如果有更多行要阅读,则更多返回true。
  4. 它将数据缓存在1024个char块中 - 因此速度很快,您可能希望为非常大的文件增加此大小。
  5. 享受!

答案 3 :(得分:2)

您的代码出现问题。这是我的版本。因为它的&#39;一个日志文件,可能正在写入它,所以最好确保你没有锁定它。

你走到最后。开始向后阅读,直到达到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();
            }
        }

答案 4 :(得分:1)

这绝不是最佳选择,但对于使用小型日志文件进行快速和脏检查我一直在使用类似的东西:

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

答案 5 :(得分:0)

现在可以在C#4.0中轻松完成的事情(在早期版本中只需要一点点努力)就是使用内存映射文件进行此类操作。它是大文件的理想选择,因为您只能映射文件的一部分,然后将其作为虚拟内存访问。

good example here

答案 6 :(得分:0)

您的日志是否有类似长度的行?如果是,那么您可以计算线的平均长度,然后执行以下操作:

  1. 寻求end_of_file - lines_needed * avg_line_length(previous_point)
  2. 阅读所有内容直至最后
  3. 如果你抓住了足够的线,那没关系。如果不是,请寻求previous_point - lines_needed * avg_line_length
  4. 读取所有内容直到previous_point
  5. 转到3
  6. 内存映射文件也是一个不错的方法 - 映射文件的尾部,计算行,映射前一个块,计算行等,直到获得所需的行数

答案 7 :(得分: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;
        }
    }

答案 8 :(得分:0)

大多数日志文件都有一个日期时间戳。虽然可以改进,但如果您想要过去 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");
    }
相关问题