0

I have this method to read a log file

 public static string GetFileData()
    {

        FileStream logFileStream = new FileStream(ConfigurationManager.AppSettings["LogFileLocation"].ToString(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        StreamReader logFileReader = new StreamReader(logFileStream);
        string line = "";
        while (!logFileReader.EndOfStream)
        {
            line += logFileReader.ReadLine()+"\n";

        }

        // Clean up
        logFileReader.Close();
        logFileStream.Close();
        return line;
    }

but now instead of showing complete log file i just want to show user last 10-20 lines as log file becomes very much heavy so its taking too much time to read complete log file.

rahularyansharma
  • 11,156
  • 18
  • 79
  • 135

1 Answers1

1

Your code has a couple of performance issues that might address your problem. If your log files aren't huge, you'll find that using the StringBuilder class to accumulate your string values will be much faster, especially if you construct your StringBuilder with the size of your file. Even faster is just do this:

var log = System.IO.File.ReadAllLines("filename");

If you really just want the last 10 lines or so you can do this:

var log = System.IO.File.ReadLines(filename).Reverse().Take(10);

There aren't any particularly good ways to read streams backwards. If the above doesn't solve your problems, you will have to resort to positioning the file pointer 'near' the end of the file and trying to figure out if you have the last n lines, if not you have to position the file pointer again (basically write code that guesses, then refines the guess ... all the while new log lines may be being added).

Here is an example of using the StringBuilder class

        System.Text.StringBuilder lines = new System.Text.StringBuilder();
        while (!logFileReader.EndOfStream)
        {
        lines.AppendLine(logFileReader.ReadLine());
        }

This will speed up you code considerably.

Sorry it may be that the System.IO.File.Read*Lines methods require exclusive access to the targeted file. You can write your own version of ReadLines that works like your above code like so:

    public IEnumerable<string> ReadLines(string filename) {
        FileStream logFileStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        using (StreamReader logFileReader = new StreamReader(logFileStream)) {
            while (!logFileReader.EndOfStream) {
                yield return logFileReader.ReadLine();
                }
            }
        }
Dweeberly
  • 4,668
  • 2
  • 22
  • 41