2013-02-19 2 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/)"합의가 "아니오, 그렇지 않아야합니다"로 표시되어야합니다. –

+0

로그 파일의 값이 쉼표로 구분되어 있습니까? 그렇다면 목록으로 값을 나눠서 목록 을 datagridview에 바인딩하거나 데이터 집합이나 데이터 테이블에 값을 읽어 들이고 그런 식으로 바인딩하십시오. – MethodMan

+0

쉼표로 구분 된 메이트가 없습니다. 파일의 한 줄입니다. 그것은 그것을 더 어색하게 만든다. 어떻게 접근해야하는지 확신 할 수 없다. – Derek

답변

0

사용자 지정 목록을 만들고 DataGridView에 바인딩하는 DJ Kraze의 접근 방식을 제안합니다. 커스텀 코드를 사용하여 라인을 파싱하면됩니다. DataTable도 사용해야하지만, List 접근법은 일반적으로 더 깨끗합니다.

(I 예를 들어, 단지, 당신은 텍스트 리더가 필요합니다 정확한 구문 또는 메서드 호출을 확인, 그래서 예를 들어 사용하지 않은)처럼 뭔가 :

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

+0

예제로 사용하는 StringParser 클래스의 단축 버전을 추가했습니다. 그것을 조정해야 할 수도 있지만 일반적으로 문자열 함수를 사용하는 방법에 대한 아이디어를 얻을 수 있습니다. – gabnaim

+0

누락 된 부분을 추가하고있었습니다. 기본적으로 문자열 함수 IndexOf()를 사용하여 시작 및 끝 위치를 찾은 다음 Substring()을 사용하여 사이에 문자열을 가져옵니다. – gabnaim