2014-11-04 58 views
1

與讀取按照以下格式此C#:行= StreamReader.ReadLine <=項目

2014/11/03 14:31:03  PID:8696  UUID:2ae855da-37d1-4a99-8510-27539afa09d0  Start E:/I3/IC/Logs/2014-11-03/SIPEngine_2.ininlog 
2014/11/04 00:00:01  PID:8696  UUID:2ae855da-37d1-4a99-8510-27539afa09d0  End  E:/I3/IC/Logs/2014-11-03/SIPEngine_2.ininlog 

我已經剝離出來的時候在一個變量,但需要獲得能夠在文件中搜索一個文件工作任何小於或等於該值

由於@dbc

現在我需要獲得最新的比賽

while (reader.next(msg, control)) 
{ 
    ActiveAttributesVect_t attribs = msg.get_active_context_attributes(); 
    ActiveAttributeValuesVect_t attribVals = msg.get_active_context_attribute_values(); 
    int numAttribs = ((attribs != null) && (attribVals != null)) ? Math.Min(attribs.Count, attribVals.Count) : 0; 
    for (int i = 0; i < numAttribs; ++i) 
    { 
    if (attribVals[i].ToString().Contains(strCallID)) 
    { 
     callidList.Add(msg.expand_format_message()); 
     // Setting the date to match the log format 
     strCallDate = msg.timestamp().as_creator_time(header.tz_offset()).ToString().Substring(0, 10).Replace("-", "/"); 
     strCallTime = msg.timestamp().as_creator_time(header.tz_offset()).ToString().Substring(11, 8); 



        Console.WriteLine("Call Time : {0}", strCallTime); 
        Console.WriteLine("Call Date : {0}", strCallDate); 
        Console.WriteLine(""); 
        foreach (var entry in callidList) 
        { 
         Console.WriteLine(entry); 
        } 
        Console.WriteLine(""); 


        OutputLogLinesBeforeTime(strLogDirectory, msg.timestamp().as_creator_time(header.tz_offset()), strCallTime); 

       } 
      } 
+1

旁註:你沒有關閉您的文件,據我可以看到。你甚至不需要StreamReader,看看'System.IO.File.ReadLines(string fileName)'。 – 2014-11-04 22:09:29

+2

那麼你的問題是什麼? – 2014-11-04 22:10:13

+0

在裁剪出的時間內使用['DateTime.Parse()'](http://msdn.microsoft.com/zh-cn/library/system.datetime.parse%28v=vs.110%29.aspx)那麼只需使用[比較運算符](http://msdn.microsoft.com/zh-cn/library/ff986512%28v=vs.110%29.aspx)作爲DateTime。 – dbc 2014-11-04 22:10:51

回答

1

您可以使用TimeSpan提取和分析一天的時間:

static TimeSpan? ExtractTime(string logLine) 
    { 
     var tokens = logLine.Split(new char [] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); 
     if (tokens.Length < 2) 
      return null; 
     TimeSpan time; 
     if (!TimeSpan.TryParse(tokens[1], out time)) 
      return null; 
     return time; 
    } 

    static DateTime? ExtractDate(string logLine) 
    { 
     var tokens = logLine.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); 
     if (tokens.Length < 1) 
      return null; 
     DateTime date; 
     if (!DateTime.TryParse(tokens[0], out date)) 
      return null; 
     return date; 
    } 

    static void OutputLogLinesBeforeTime(string strLogDirectory, string strLogDate, string strCallTime) 
    { 
     try 
     { 
      var time = TimeSpan.Parse(strCallTime); // Throws a format exception if invalid. 

      DirectoryInfo d = new DirectoryInfo(strLogDirectory + "\\" + strLogDate + "\\"); 

      foreach (var file in d.GetFiles("*.ininlog_journal")) 
      { 
       try 
       { 
        using (Stream stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 
        using (StreamReader sReader = new StreamReader(stream)) 
        { 
         foreach (var line in sReader.EnumerateLines().Where(l => ExtractTime(l) < time)) 
          Console.WriteLine(line); 
        } 
       } 
       catch (UnauthorizedAccessException ae) 
       { 
        Console.WriteLine(ae.Message); 
       } 
       catch (SystemException se) 
       { 
        Console.WriteLine(se.Message); 
       } 
       catch (ApplicationException ape) 
       { 
        Console.WriteLine(ape.Message); 
       } 
       catch (Exception e) 
       { 
        Console.WriteLine(e.Message); 
       } 
      } 
     } 
     catch (UnauthorizedAccessException ae) 
     { 
      Console.WriteLine(ae.Message); 
     } 
     catch (SystemException se) 
     { 
      Console.WriteLine(se.Message); 
     } 
     catch (ApplicationException ape) 
     { 
      Console.WriteLine(ape.Message); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.Message); 
     } 
    } 

爲了方便我摘錄如下:

public static class TextReaderExtensions 
{ 
    public static IEnumerable<string> ReadLines(this TextReader sReader) 
    { 
     if (sReader == null) 
      throw new ArgumentNullException(); 
     string line; 
     while ((line = sReader.ReadLine()) != null) 
      yield return line; 
    } 
} 
+1

將試一試,讓你知道 – ondrovic 2014-11-05 01:13:18

+0

實施你提供的代碼,由於某種原因,雖然沒有線路輸出時看到的logline這裏是我所看到的\t \t logLine \t「2014/11/03 00:00:22 \ tPID:4332 \ tUUID:36515761-7ea8-4628-b137-2b4bed4ae7e5 \ tStart \ tE:/I3/IC/Logs/2014-11-03/caasbillingserver.ininlog「\t string – ondrovic 2014-11-05 13:28:22

+0

@ondrovic - 您的日誌文件單元格是分隔的通過標籤和空格;更新'ExtractTime'以使用任一字符進行分割。 – dbc 2014-11-05 15:49:25