简体   繁体   中英

Get all lines after the last print of line with 'keyword' in C#

I am working on ac# project. I am trying to send a logfile via email whenever application gets crashed. however logfile is a little bit larger in size.

So I thought that i should include only a specific portion of logfile.

For that I am trying to read all the lines after the last instance of line with specified keyword.(in my case "Application Started")

since Application get restarted many times(due to crashing), 'Application Started' gets printed many times in file. So I would only want last print of line containing 'Application Started' & lines after that until end of file.

I require help to figure out how can i do this.

I have just started with Basic code as of now.

System.IO.StreamReader file = new System.IO.StreamReader("c:\\mylogfile.txt");
while((line = file.ReadLine()) != null)
{
    if ( line.Contains("keyword") )
    {

    }

}

Read the file, line-by-line, until you find your keyword. Once you find your keyword, start pushing every line after that into a List<string> . If you find another line with your keyword, just Clear your list and start refilling it from that point.

Something like:

List<string> buffer = new List<string>();
using (var sin = new StreamReader("pathtomylogfile")) 
{
    string line;
    bool read;
    while ((line = sin.ReadLine())!=null) 
    {
        if (line.Contains("keyword"))
        {
            buffer.Clear();
            read = true;
        }
        if (read)
        {
            buffer.Add(line);
        }
    }
    // now buffer has the last entry
    // you could use string.Join to put it back together in a single string
    var lastEntry = string.Join("\n",buffer);
}

If the number of lines in each entry is very large, it might be more efficient to scan the file first to find the last entry and then loop again to extract it. If the whole log file isn't that large, it might be more efficient to just ReadToEnd and then use LastIndexOf to find the start of the last entry.

Read everything from the file and then select the portion you want.

   string lines = System.IO.File.ReadAllText("c:\\mylogfile.txt");
   int start_index = lines.LastIndexOf("Application Started");
   string needed_portion = lines.Substring(start_index);
   SendEmail(needed_portion);

I advise you to use a proper logger, like log4net or NLogger . You can configure it to save to multiple files - one containing complete logs, other containing errors/exceptions only. Also you can set maximum size of log files, etc. Or can configure them to send you a mail if exception occours.

Of course this does not solves your current problem, for it there is some solution above.

But I would try simpler methods, like trying out Notepad++ - it can handle bigger files (last time i've formatted a 30MB XML document with it, it took about 20 mins, but he did it! With simple text files there should be much better perf.). Or if you open the file for reading only (not for editing) you may get much better performance (in Windows).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM