Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse a log file and get entry data

Tags:

c#

file

logging

I have a big log file with multiple lines separated by new line. Each line entry stores four values.

If I am reading the log file and want to store this information, what data type/object should I use?

Example:

source1 destination1 result time
source2 destination1 result time
sources3 destination2 result time

The values are not unique between lines and they can repeat.

Can I declare a struct for each line with {source, destination, result, time} values and store the struct object in a List<T> for the entire file?

like image 357
user393148 Avatar asked May 30 '26 11:05

user393148


1 Answers

Yes, you can take the approach of creating your own specific class and making a List of that class.

Depending on how large your file is, you could do better by using a DataTable to do this:

var dt = new DataTable();
dt.Columns.Add(new DataColumn("Source", typeof(string)));
dt.Columns.Add(new DataColumn("Destination", typeof(string)));
dt.Columns.Add(new DataColumn("Result", typeof(string)));
dt.Columns.Add(new DataColumn("Timestamp", typeof(DateTime)));

var dr = dt.NewRow();
dr["Source"] = "DATA";
dr["Destination"] = "DATA";
dr["Result"] = "DATA";
dr["Timestamp"] = DateTime.Now;
dt.Rows.Add(dr);
like image 168
palswim Avatar answered May 31 '26 23:05

palswim