使用Linq解析文本文件

时间:2010-09-18 09:48:12

标签: linq

我有一个下面格式的日志文件,因为你可以看到每个日志都以时间开始并以管道分隔符结束。

将每个日志以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|

文件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



        ============================================

程序:

  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
}

1 个答案:

答案 0 :(得分:6)

您可能想尝试一下:

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]
    };

这个简单的辅助方法:

private static bool IsDateTime(string time)
{
    DateTime temp;
    return DateTime.TryParse(time, out temp);
}

更新: 当您使用.NET 4.0时,您应该使用File.ReadLines而不是File.ReadAllLines。这可以防止将整个文件加载到内存中。