[英]Display log file information on a Web page with ASP.NET MVC paging
I have logs stored in a txt file in the following format. 我将日志存储在txt文件中,格式如下。
======8/4/2010 10:20:45 AM=========================================
====== 8/4/2010 10:20:45 AM ================================= ========
Processing Donation
处理捐赠
======8/4/2010 10:21:42A M=========================================
====== 8/4/2010 10:21:42A M ================================= ========
Sending information to server
将信息发送到服务器
======8/4/2010 10:21:43 AM=========================================
====== 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 page using paging in ASP.NET MVC. 我需要将这些行解析为一个列表,其中“====”行之间的信息被计为一个记录,以便在ASP.NET MVC中使用分页显示在网页上。
Example: The first record entry would be 示例:第一个记录条目是
======8/4/2010 10:20:45 AM=================================================
====== 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. 我总是忘记正则表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.