2013-08-01 1 views
0

로그 .txt 파일을보고 오류 메시지의 모든 인스턴스를 찾은 다음 C#을 사용하여 오류 줄을 새 문서에 붙여넣고 기본적으로 오류 줄만있는 별도의 .txt 파일을 만듭니다. 나열된 로그 파일txt 문서에서 단어를 검색하고보고 할 단어를 새 문서에 쓰려면 어떻게해야합니까?

C#을 사용하여 문서에서 텍스트를 검색하는 방법을 알고 있지만 전체 나머지 부분을 추가하지 않고 (일반적으로 1-2 줄이 아닌) 해당 오류 메시지를 추출하는 가장 좋은 방법은 무엇입니까? 첫 번째 오류 인스턴스 이후 문서?

편집

로그 파일은 각 라인에 이벤트를 기록, 다음과 같이 오류 라인 읽기 :이 도움이된다면

Running install update (3) 
ERROR: Running install update(3) (ERROR x34293) The system was unable to update the application, check that correct version is accessible by the system. 
Running install update (4) 

은 알려 주시기 바랍니다 . 같은

+1

우리는 입력의 예를 볼 수 있다면 도움이 될, 그리고 예상되는 출력의 예는 – musefan

+2

그것은 보지 않고 대답을 공식화하는 것은 매우 어렵다 파일의 예입니다. –

답변

2

뭔가 : 당신은 라인 내에서 특정 정보를 필요로하는 경우

foreach(var line in File.ReadLines(filename) 
         .Where(l => l.Contains("ERROR MESSAGE"))) 
{ 
    // Log line 
} 

은 또한 당신은 정보를 캡처하는 Regex를 사용할 수 있습니다. 더 많은 정보가없는 좋은 예를 제공 할 수는 없습니다.

1

당신은 당신을 발견 문자 위치, 또는 줄 번호 (기억이 안나요) 중 하나를 제공 정규식 패턴을 통해 파일을 검색 할 수 있습니다. 그런 다음 RegEx에서 반환되는 부분을 가져올 수 있습니다.

여기에 내가 그 정규식을 사용하여 쓴 내 자신의 "찾기 및 바꾸기"프로그램에서 코드 블록입니다. ...이 일부 중첩 방법은,하지만 당신은 요점을 파악

int foundInThisFile; 
string regExPattern = FindText; 
System.Text.RegularExpressions.Regex regSearch = null; 

if (IgnoreCase) 
    regSearch = new System.Text.RegularExpressions.Regex(regExPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline); 
else 
    regSearch = new System.Text.RegularExpressions.Regex(regExPattern, System.Text.RegularExpressions.RegexOptions.Multiline); 

      System.Text.RegularExpressions.MatchCollection regExMatches = regSearch.Matches(reader.ReadToEnd()); 

      if (reader != null) 
      { 
       reader.Dispose(); 
       reader = null; 
      } 

      found += regExMatches.Count; 
      TotalMatches(new CountEventArgs(found)); 

      foundInThisFile = regExMatches.Count; 
      MatchesInThisFile(new CountEventArgs(foundInThisFile)); 

      if (regExMatches.Count > 0) 
      { 
       foreach (System.Text.RegularExpressions.Match match in regExMatches) 
       { 
        // The first "group" is going to be the entire regex match, any other "group" is going to be the %1, %2 values that are returned 
        // Index is the character position in the entire document 
        if (match.Groups.Count > 1) 
        { 
         // This means the user wants to see the grouping results 
         string[] groupsArray = new string[match.Groups.Count - 1]; 

         for (int counter = 1; counter < match.Groups.Count; counter++) 
          groupsArray[counter - 1] = match.Groups[counter].Value; 

         int lineNumber = 0; 
         string actualLine = String.Empty; 

         GetLineNumberAndLine(localPath, match.Groups[0].Index, out lineNumber, out actualLine); 

         AddToSearchResults(localPath, lineNumber, actualLine, groupsArray); 

         NewSearchResult(new SearchResultArgs(new FindReplaceItem(localPath, lineNumber, actualLine, ConvertGroupsArrayToString(groupsArray)))); 
        } 
        else 
        { 
         int lineNumber = 0; 
         string actualLine = String.Empty; 

         GetLineNumberAndLine(localPath, match.Groups[0].Index, out lineNumber, out actualLine); 

         AddToSearchResults(localPath, lineNumber, actualLine); 

         NewSearchResult(new SearchResultArgs(new FindReplaceItem(localPath, lineNumber, actualLine))); 
        } 
       } 
      } 
관련 문제