2016-09-12 11 views
0

私は単純なアプリケーションでSystem.Diagnostic.Process.Application "Y"を使用して外部プログラム "Y"を起動する "X"実行すると、アプリケーションログがテキストファイルに書き込まれます。C#の外部プログラムによって作成されたログファイルからrichtextboxへカスタムログを書き込む方法

今、私はそのApp Yログを読んでいます。ログには、ユーザーが入力して自分の言葉に置き換えてリッチテキストボックスに追加するのに難しい単語がいくつか含まれています。言葉に続き、追加として、彼らは

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

を次のようにログファイル自体は今、私は上記のそれぞれの行を交換したいと思い読み込むログファイル

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(); 
     } 
    } 

に書かれているよう 私の現在のコードは、読み込み私自身のカスタム単語

と私は、この

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

自分のコードを使用してリッチテキストボックスに何も表示されないことは非常に悲しいです。rtbLog.Text = str;コードからもコメントされました! –

+0

あなたのアプリで使用しているコードにコードを更新してみることができます。 –

+0

更新されたコードは次のようになります http://pastebin.com/93XxnF77 –

0

あなたはSelectedTextにテキストを追加ちょうどその値を設定しないでください。 代わりの

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

を行います
rtbLog.SelectedText= "Application engine Started"; 

など。

もう1つのコメントは、質問とは関係ありませんが、ストリームを使用してパターンを使用してみてください。

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

ログファイルを1行ずつ読み込むと、次のコードのように問題は解決しますが、最適なパフォーマンスは得られません。

ログファイル形式が比較的シンプルな場合は、ストリーム全体を一度に文字列として処理し、正規表現を使用してパターンマッチングを検討することができます。

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()を使用する必要はありません。とdispose();これはコードの一番下にあります。 しかし、ログの読み方に関する提案はうまくいかない –

関連する問題