我将日志以下列格式存储在 txt 文件中。
======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=========================================
我需要将这些行解析为一个列表,其中“====”行之间的信息被计为一条记录,以使用 ASP.NET MVC 中的分页显示在网页上。
示例:第一个记录条目将是
======8/4/2010 10:20:45 AM=================================================
Processing Donation
到目前为止我运气不好。我该怎么做?
最佳答案
在读取文件时,你能检查一下该行是否以 ===== 结尾吗
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);
}
}
有点冗长,但可能会给你一些想法
关于c# - 使用 ASP.NET MVC 分页在网页上显示日志文件信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3498468/