so I’m trying to upload then parse a text file with the following format:
Mar 29 19:23:51,667|DEBUG|1 |1: Initializing lorem ipsum…
Mar 29 19:23:31,682|ERROR|1 |1: Lorem.Ipsum.Exception while starting Foo.Bar.Launcher
System.Blah.LoremException: Lorem ipsum dolor sit amet, consectetur adipisicing elit…
at System.Lorem.Ipsum.Dolor.foo()
at System.Lorem.Ipsum.Dolor.foo()
…
Mar 30 22:23:23,667|DEBUG|1 |1: Initializing lorem ipsum…
Apr 02 17:24:17,413|ERROR|4 |4: Lorem failed to ipsum… System.DolorException: Object reference not set to an instance of an object.
at Lorem.Ipsum.Dolor.IpsumDbController..ctor()
And the Error class:
public class Error
{
public int ID { get; set; }
public string Date { get; set; }
public string Description { get; set; }
public string ErrorType { get; set; }
}
Where There are Two Errors:
Error 1
Mar 29 19:23:33 – is the Date
System.Blah.LoremException – is the ErrorType.
Lorem ipsum dolor sit amet, consectetur adipisicing elit – is the Description
Error 2
Apr 02 17:24:17 – is the Date
System.DolorException – is the ErrorType.
Object reference not set to an instance of an object. – is the Description
Is there an easy way I can parse the string (through regex? or not?)? I was thinking of splitting the string if it contains ERROR, then getting the next line to assign to ErrorType.
I’m not quite sure how I would go with this, so any help will be appreciated!
UPDATE : The pattern is really inconsistent, so I’m not really confident with the String.Split solution.
The general rule is:
All |ERROR| will have a Date (our string Date), System.blah.LoremException (our ErrorType) followed by an Exception message (our Description)
The ErrorType & Description could possibly be inline with the ERROR string or on the next line.
I would use a combination of a StreamReader and Regular Expressions to handle parsing.
The logic behind this is basically to iterate through the stream line-by-line searching for a match to the regular expression. The regular expression itself is tailored to fit only “ERROR” lines, so it won’t match on “DEBUG” etc.
If the line matches the expression, a new “Error” class instance is put into the list, and the parsed values from the Regular Expression are used to populate the fields. To fill the “ErrorType” field, I simply read the next line following the match.
EDIT
Okay, the best way I can see is by matching the trailing “…” at the end of the error messages when the exception is on the same line, then attempting to match further.
Revised code: