tags:

views:

73

answers:

1

Hi all I have a log file in the format below as you can see each log start with a time and ends with pipe delimeter.

Put each Log starting with a dateTime and ending with a pipe delimeter in a List

How can I parse this text file and put the logs in the collections? I seem to have a problem in determine how can I find the start and end of a log and read it each log

Below is a quick example to give an idea of what I am trying to do. Any pointers help etc... really appreciated

Log example

        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|

File 2 Scenario My Order

        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



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

Program:

  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
}
+2  A: 

You might want to try this:

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

And this simple helper method:

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

UPDATE: And when you use .NET 4.0, you should use File.ReadLines instead of File.ReadAllLines. This prevents loading the whole file into memory.

Steven
I use this pattern all the time. It's great for processing CSV or any structured text file.
Slaggg
Hi Steven. It works like a charm!!! thanks a lot. can I push my luck and ask you another question? I have another scenario where I need to read a file again but this time Is a file containing orders and each order is divided with a "=================How can I possibly read again the file and put in a collection again. See edited question.Again Thank you
That's a very scary format you’ve got there. Are you sure you want to parse this? Try changing the input file to something more safe to parse (XML for instance). Also, while I think it might be possible to do with LINQ, you're probably better of using a more imperative approach to parse such an approach. Good luck.
Steven
Thanks a lot for your help.Cannot change format as I have no control.But I will find another way