Parsing a text file using Linq

I have a log file in the following format, since you can see each run in the log over time and end with a pipe metric.

Put each log starting with dateTime and ending with a tunnel in the list

How can I parse this text file and put the logs in collections? It seems that I have a problem in determining how I can find the beginning and end of a journal and read it every journal

The following is a brief example to give an idea of ​​what I'm 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
}
+3
source share
1 answer

, :

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

UPDATE: .NET 4.0, File.ReadLines File.ReadAllLines. .

+7

Source: https://habr.com/ru/post/1765410/


All Articles