我有一个下面格式的日志文件,因为您可以看到每个日志以时间开始并以管道分隔符结束.
将每个日志以dateTime开头,并以List中的管道分隔符结束
如何解析此文本文件并将日志放入集合中?我似乎在确定如何找到日志的开头和结尾并在每个日志中读取它时遇到问题
下面是一个简单的例子来说明我想要做的事情.任何指针帮助等...真的很感激
08:52:03.260|Error| Stack Trace and other info removed here|
lots of info about the stack trace
lots of info about the stack trace
lots of info about the stack trace
lots of info about the stack trace
lots of info about the stack trace|
09:52:03.260|Error| Stack Trace and other info removed here|
lots of info about the stack trace
lots of info about the stack trace
lots of info about the stack trace
lots of info about the stack trace
lots of info about the stack trace|
09:52:03.260|Error|Stack Trace and other info removed here|
lots of info about the stack trace
lots of info about the stack trace
lots of info about the stack trace
lots of info about the stack trace
lots of info about the stack trace|
Run Code Online (Sandbox Code Playgroud)
文件2方案我的订单
Quantity Description Price
1 shoes £1.00
Total £1.00
No: 34343345
=============================================
My Order
Quantity Description Price
1 TShirt £1.00
Total £1.00
No: 32234234
============================================
Run Code Online (Sandbox Code Playgroud)
程序:
class Program
{
static void Main(string[] args)
{
string path = @"MyTestLog.log";
string aa = string.Empty;
List<LogMessage>logMessages=new List<LogMessage>();
using (StreamReader reader = new StreamReader(path))
{
//????
logMessages.Add(new LogMessage
{
Time = ??,
ErrorLevel = ,
Details = ??
});
}
}
}
public class LogMessage
{
public DateTime Time { get; set; }
public string ErrorLevel { get; set; }
public string Details { get; set; }
//other stuff here
}
Run Code Online (Sandbox Code Playgroud)
你可能想试试这个:
var list =
from line in File.ReadAllLines("log.txt")
where line.EndsWith("|")
let parts = line.Split('|')
where parts.Length >= 2
where IsDateTime(parts[0])
select new LogMessage()
{
Time = DateTime.Parse(parts[0]),
ErrorLevel = parts[1],
Details = parts[2]
};
Run Code Online (Sandbox Code Playgroud)
而这个简单的帮助方法:
private static bool IsDateTime(string time)
{
DateTime temp;
return DateTime.TryParse(time, out temp);
}
Run Code Online (Sandbox Code Playgroud)
更新:当您使用.NET 4.0时,您应该使用File.ReadLines而不是File.ReadAllLines.这可以防止将整个文件加载到内存中.
| 归档时间: |
|
| 查看次数: |
2648 次 |
| 最近记录: |