views:

85

answers:

4

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?

+2  A: 

Roll your own class

public class LogEntry
{
    public string Source;
    public string Destination;
    public DateTime ResultTime;
}

You could then call Split() on each entry line, then parse each element of the String[] into your LogEntry objects.

jlafay
+3  A: 

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);
palswim
+3  A: 

Yes that is exactly what I would do. If you don't have LINQ you can use generic List with a custom class of your own.

public class LogEntry
{
     private String _source = String.Empty;
     private String _destination = String.Empty;
     private String _result = String.Empty;
     private String _time = String.Empty;

     public String Source
     {
         get { return _source; }
         set { _source = value; }
     }

     public String Destination
     {
            get { return _source; }
            set { _source = value; }
     }

     public String Result
     {
         get { return _source; }
         set { _source = value; }
     }

     public String Time
     {
         get { return _source; }
         set { _source = value; }
     }

     public LogEntry()
     {
     }

     public LogEntry( String source, String destination, String result, String time )
     {
         _source = source;
         _destination = destination;
         _result = result;
         _time = time;
     }

    public LogEntry( String[] args )
    {
        _source = args[0];
        _destination = args[1];
        _result = args[2];
        _time = args[3];
    }
  }

You can then use a list like so:

 List<LogEntry> _logEntries = new List<LogEntry>();

I would read the each line from the file into a String and create a new LogEntry class passing in the result from a String.Split method call. The code below is strictly psuedo code.

char[] splitter = new char[1];
splitter[0] = ' ';

while( readLines into String )
{
   _logEntries.Add( new LogEntry( String.Split( splitter ) ) );
}

This solution is extremely brittle and any change in the log file format will horribly break the code but it will work. Also your breaking on white space so you better make sure that's what you really want to break on.

Justin
+2  A: 
        class YourType
        {
            public String Source { get; private set; }
            public String Destination { get; private set; }
            public String Resoult { get; private set; }
            public String Time { get; private set; }

            //or func returning bool and remove exception
            public YourType(String line)
            {
                var split = line.Split(' ');

                if (split.Length != 4)
                    throw new ArgumentException();

                Source = split[0];
                Destination = split[1];
                Resoult = split[2];
                Time = split[3];
            }

        }

        public Main()
        {
            String values =
 @"source1 destination1 result time
source2 destination1 result time
sources3 destination2 result time";



            var v = from line in values.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
                    select new YourType(line);

            foreach (var it in v)
            {
                Console.WriteLine(it.Source);
            }

        }
nilphilus