2013-02-19 57 views
0

我有一個日誌文件,我想在DataGridView中呈現。將日誌文件讀入DataGridView

爲例線文件中的數據的將是: -

<![LOG[Creating mandatory request for advert 0002124C, program Shutdown Desktops Overnight, package 0000073C]LOG]!><time="05:00:00.192+000" date="02-11-2013" component="execmgr" context="" type="1" thread="3712" file="execreqmgr.cpp:2858"> 

欲拉出示例的方面的上方,記錄說明,時間&日期,組件,上下文,類型和主題..並將它們添加爲DataGridView的DataSource中的列。

哪種方法可以解決這些問題?

+0

我編輯了自己的冠軍。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 – 2013-02-19 14:33:59

+0

日誌文件逗號中的值是否分開?如果是這樣,將值分成一個列表和綁定列表到datagridview或讀取到數據集或數據表中的值,並綁定他們的方式.. – MethodMan 2013-02-19 14:34:20

+0

沒有逗號隔離隊友,正如我上面張貼。這是文件中的一行。它使它更尷尬,不知道如何處理它。 – Derek 2013-02-19 14:36:21

回答

0

我建議DJ Kraze的方法:創建一個自定義列表並將其綁定到您的DataGridView。只需使用自定義代碼來解析該行。如果必須,也可以使用DataTable,但List方法通常更清晰。

喜歡的東西(我沒有檢查的確切語法或方法調用,所以作爲一個例子只使用,您將需要一個文本閱讀器,例如):

public class LogEntry { 
    public string Description { get; set; } 
    public DateTime LogDate { get; set; } 
    // other properties you want to extract 
} 

public class LogReader { 
    public List<LogEntry> ReadLog(string fileName){ 
     var parsedLog = new List<LogEntry>(); 
     using(var file = File.Open(filename, ....)){ 
      while(var line = file.ReadLine()){ 
       var logEntry = ParseLine(line); 
       parsedLog.Add(logEntry); 
      } 
     } 
     return parsedLog; 
    } 

    private LogEntry ParseLine(string line){ 
     var logEntry = new LogEntry(); 
     // go through the line and parse it with string functions and populate values. 
     // I use a helper class, a shortened version is below - note, you might need to 
     //adjust to compile 
     var parser = new StringParser(line); 
     //now just use GetBetween to find the values you need. just pick your delimiters 
     // carefully as the parser will move beyond the end string. But it will go 
     // sequentially so just experiment 
     logEntry.Description = parser.GetBetween("LOG[", "]LOG"); 
     // go through in order 
    } 
} 

public class StringParser { 
    private string text; 
    private int position; 

    public StringParser(string text) 
    { 
     this.Text = text; 
    } 

    public string Text 
    { 
     get { return this.text; } 
     private set 
     { 
      this.text = value; 
      Position = 0; 
     } 
    } 

    public int Position 
    { 
     get { return this.position; } 
     private set 
     { 
      if (value < 0) 
      { 
       this.position = 0; 
      } 
      else 
      { 
       this.position = value > this.Text.Length ? this.Text.Length : value; 
      } 
     } 
    } 

    public bool AtEnd 
    { 
     get { return this.Position >= this.Text.Length; } 
    } 

    public string GetBetween(string beforeText, string afterText) 
    { 
     var startPos = MoveAfter(beforeText); 
     if (startPos == -1) 
     { 
      return ""; 
     } 

     var endPos = FindNext(afterText); 
     return GetBetween(startPos, endPos); 
    } 

    public string PeekBetween(int startPos, int endPos) 
    { 
     if (startPos < 0 || startPos >= this.text.Length) 
     { 
      return ""; 
     } 
     if (endPos < 0 || endPos > this.text.Length) 
     { 
      endPos = this.text.Length; 
     } 
     if (startPos >= endPos) 
     { 
      return ""; 
     } 

     var result = this.text.Substring(startPos, endPos - startPos); 
     return result; 
    } 

    public string GetBetween(int startPos, int endPos) 
    { 
     if (endPos < 0 || endPos > this.text.Length) 
     { 
      endPos = this.text.Length; 
     } 
     var result = PeekBetween(startPos, endPos); 
     if (!string.IsNullOrEmpty(result)) 
     { 
      this.Position = endPos; 
     } 
     return result; 
    } 


    public int FindNext(string searchText) 
    { 
     if (string.IsNullOrEmpty(searchText) || this.AtEnd) 
     { 
      return -1; 
     } 
     return this.text.IndexOf(searchText, this.Position, StringComparison.Ordinal); 
    } 

    public int MoveAfter(string searchText) 
    { 
     var found = FindNext(searchText); 
     if (found > -1) 
     { 
      found += searchText.Length; 
      this.Position = found; 
     } 
     return found; 
    } 
} 
+0

感謝這個,它的解析部分我正在努力 – Derek 2013-02-19 15:34:07

+0

我添加了一個縮短版本的StringParser類作爲例子。您可能需要對其進行調整,但通常您可以瞭解如何使用字符串函數。 – gabnaim 2013-02-19 15:58:22

+0

我正在添加缺少的部分。基本上你使用字符串函數IndexOf()來查找開始和結束位置,然後使用Substring()來獲取字符串。 – gabnaim 2013-02-19 16:03:58