开发者

Parse a log file and get entry data

开发者 https://www.devze.com 2023-01-15 11:07 出处:网络
I have a big log file with multiple lines separated by new line. Each line entry stores fo开发者_StackOverflow中文版ur values.

I have a big log file with multiple lines separated by new line. Each line entry stores fo开发者_StackOverflow中文版ur 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?


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


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.


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

        }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消