2016-09-12 1 views
0

간단한 응용 프로그램에서 System.Diagnostic.Process.Application "Y"를 사용하여 외부 프로그램 "Y"를 시작하는 "X"라는 이름을 지정할 수 있습니다. 실행하면 응용 프로그램 로그가 텍스트 파일에 기록됩니다.C#에서 외부 프로그램에 의해 작성된 로그 파일에서 richtextbox에 사용자 정의 로그를 작성하는 방법

이제 App Y 로그를 읽으려고합니다. 로그에 사용자가 묻어 내 단어로 바꾸고 서식있는 텍스트 상자에 추가하기 어려운 단어가 포함되어있는 경우 읽으려고합니다. 다음 그들이 로그 파일

private void timerLog_Tick(object sender, EventArgs e) 
    { 
     //logpath is the path to that log file 
     if (File.Exists(logpath)) 
     { 
      Stream stream = File.Open(logpath, FileMode.Open,FileAccess.Read, FileShare.ReadWrite); 
      StreamReader streamReader = new StreamReader(stream); 
      string str = streamReader.ReadToEnd(); 

      //rtblog is my RichTextBox 
      rtbLog.Text = str; 
      rtbLog.SelectionStart = rtbLog.Text.Length; 
      rtbLog.ScrollToCaret(); 


      streamReader.Close(); 
      stream.Close(); 
     } 
    } 

지금

Mon Sep 12 19:22:56 2016 Application engine version 2.0.2016 was initiated Mon Sep 12 19:22:56 2016 Windows version 6.2 (Windows 8 or greater) Mon Sep 12 19:22:56 2016 System: 64 bits system Mon Sep 12 19:22:56 2016 checking application drivers Mon Sep 12 19:22:56 2016 Drivers not found Mon Sep 12 19:22:56 2016 Attempting to perform a task Mon Sep 12 19:22:56 2016 The task failed Mon Sep 12 19:22:56 2016 Process failed,The program is exiting

을 다음과 내가 위의 각 라인을 대체하려는 자체가 읽고 로그 파일을 기록으로 단어를 추가로 내 현재 코드 읽기 내 자신의 사용자 지정 단어

와 나는이

if (str.LastIndexOf("Application engine version 2.0.2016 was initiated")>0) 
      { 
       rtbLog.SelectedText= rtbLog.SelectedText+"Application engine Started"; 
      } 
      else if (str.LastIndexOf("Drivers not found")>0) 
      { 
       rtbLog.SelectedText= rtbLog.SelectedText+"Drivers were not found navigate to settings Menu to install them"; 
      }..... 
0과 같이 밖으로 시도

이 코드는 계속되지만이 코드는 첫 번째 줄 하나를 인쇄하는 루프에 들어갑니다.

어떤 조작을 수행해주십시오. 사전에

감사

답변

0
다음과 같은

수정 코드 :

if (File.Exists(logpath)) 
     { 
      Stream stream = File.Open(logpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
      StreamReader streamReader = new StreamReader(stream); 
      StringBuilder newString = new StringBuilder(); 

      while (!streamReader.EndOfStream) 
      { 
       string str = streamReader.ReadLine(); 

       if (str.LastIndexOf("Application engine version 2.0.2016 was initiated") > 0) 
       { 
        str = "Application engine Started"; 
       } 
       else if (str.LastIndexOf("Drivers not found") > 0) 
       { 
        str = "Drivers were not found navigate to settings Menu to install them"; 
       } 

       newString.AppendLine(str); 
      } 
      rtbLog.Text = newString.ToString(); 
      rtbLog.ScrollToCaret(); 


      streamReader.Close(); 
      stream.Close(); 
     } 
.... 
+0

그 어떤 것도 자신의 코드를 사용하여 RichTextBox에서 온 없다는 것을 매우 슬픈 rtbLog.Text = str을; 코드에서 주석 처리되었습니다. –

+0

앱에서 사용중인 코드로 코드를 업데이트하여 살펴볼 수 있습니까? –

+0

업데이트 된 코드는 다음과 같습니다. http://pastebin.com/93XxnF77 –

0

당신은 SelectedText 텍스트를 추가하지만, 단지 그 값으로 설정해서는 안 :

rtbLog.SelectedText= rtbLog.SelectedText+"Application engine Started"; 

하는 대신 가 할

rtbLog.SelectedText= "Application engine Started"; 

등등.

덧글을 한 번 더 추가하면 질문과 관련이 없지만 스트림과 함께 사용 패턴을 사용해보십시오.

using (StreamReader streamReader = new StreamReader(logpath)) 
{ 
... 
} 

다음 코드처럼 현명 최적의 성능은 아니지만, 문제를 해결, 라인별로 로그 파일 라인을 읽기.

로그 파일 형식이 비교적 간단한 경우 전체 스트림을 한 번에 문자열로 처리하고 정규 표현식을 사용하여 패턴 일치를 고려할 수 있습니다.

using (StreamReader streamReader = new StreamReader(logpath)) 
    { 
     string line; 
     while ((line = streamReader.ReadLine()) != null) 
     { 
      if (line.LastIndexOf("Application engine version 2.0.2016 was initiated") > 0) 
      { 
       richTextBox1.SelectedText = "Application engine Started\n"; 
      } 
      else if (line.LastIndexOf("Drivers not found") > 0) 
      { 
       richTextBox1.SelectedText = "Drivers were not found navigate to settings Menu to install them\n"; 
      } 
     } 
    } 
+0

네가 맞다. "키워드"를 사용한다고해서 항상 close(); 및 처분(); 이는 코드의 맨 아래에 있습니다. 그러나 로그 읽기에 대한 제안은 작동하지 않습니다. –

관련 문제