开发者

Display log file information on a Web page with ASP.NET MVC paging

开发者 https://www.devze.com 2023-01-12 08:08 出处:网络
I have logs stored in a txt file in the following format. ======8/4/2010 10:20:45 AM=========================================

I have logs stored in a txt file in the following format.

======8/4/2010 10:20:45 AM=========================================

Processing Donation

======8/4/2010 10:21:42A M=========================================

Sending information to server

======8/4/2010 10:21:43 AM=========================================

I need to parse these lines into a list where the information betweeen "====" lines is counted as one record to display on web pag开发者_如何学Goe using paging in ASP.NET MVC.

Example: The first record entry would be

======8/4/2010 10:20:45 AM=================================================

Processing Donation

I had no luck so far. How can I do it?


Whilst reading in the file could you do a check to see if the line ends with =====

var sBuilder = new StringBuilder()
bool lineEnd = false;
var items = new List<string>();
string currentLine = String.Empty
using(var file = new StringReader("log.txt"))
{
  while( (currentLine = file.ReadLine()) != null)
  {
    if(currentLine.EndsWith("===="))
    {
        items.Add(sBuilder.ToString());
        sBuilder.Clear();
    }
    else
        sBuilder.Append(currentLine);
  }
}

It's a bit verbose but might give you some ideas


So... Ignore the verbose code in my other answer. Instead use this two line wonder:

string texty = "=====........"; //File data
var matches = Regex.Matches(texty, @"={6}(?<Date>.+)={41}\s*(?<Message>.+)");

var results = matches.Cast<Match>().Select(m => new {Date = m.Groups["Date"], Message = m.Groups["Message"]});

I always forget about regular expressions.

0

精彩评论

暂无评论...
验证码 换一张
取 消