【问题标题】:Parse a log file and get entry data解析日志文件并获取条目数据
【发布时间】:2010-09-10 21:12:01
【问题描述】:

我有一个大日志文件,其中多行由新行分隔。每个行条目存储四个值。

如果我正在读取日志文件并想要存储此信息,我应该使用什么数据类型/对象?

例子:

源 1 目标 1 结果时间 源 2 目标 1 结果时间 来源3 目的地2 结果时间

这些值在行之间不是唯一的,它们可以重复。

我可以为每一行声明一个带有 {source、destination、result、time} 值的结构,并将结构对象存储在整个文件的 List<T> 中吗?

【问题讨论】:

  • 我会使用类而不是结构,结构会占用更多内存,所有值都被复制,而类实例只能引用。
  • 谢谢大家!我将使用以下方法。 :)

标签: c# file logging


【解决方案1】:

是的,您可以采用创建自己的特定类并为该类创建List 的方法。

根据您的文件有多大,您可以通过使用DataTable 来做得更好:

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

【讨论】:

    【解决方案2】:

    是的,这正是我会做的。如果您没有 LINQ,则可以将通用 List 与您自己的自定义类一起使用。

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

    然后您可以使用这样的列表:

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

    我会将文件中的每一行读入一个字符串,并创建一个新的 LogEntry 类,传入来自 String.Split 方法调用的结果。下面的代码是严格的伪代码。

    char[] splitter = new char[1];
    splitter[0] = ' ';
    
    while( readLines into String )
    {
       _logEntries.Add( new LogEntry( String.Split( splitter ) ) );
    }
    

    此解决方案非常脆弱,日志文件格式的任何更改都会严重破坏代码,但它会起作用。还有你在空白处的突破,所以你最好确保那是你真正想要突破的地方。

    【讨论】:

      【解决方案3】:
              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);
                  }
      
              }
      

      【讨论】:

        【解决方案4】:

        滚动你自己的课程

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

        然后您可以在每个输入行上调用 Split(),然后将 String[] 的每个元素解析到您的 LogEntry 对象中。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-04-20
          • 1970-01-01
          • 1970-01-01
          • 2023-03-29
          • 1970-01-01
          • 2013-11-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多