Reputation: 45
I have the simple application lets name it "X" which launches external program "Y" using System.Diagnostic.Process.Application "Y" while executing writes the application log to the text file.
Now I want to read that App "Y" log and if the Log Contains some words which are hard for the user to entepret and replace them to my own words and append them to the rich textbox. My current code reads as follows and appends the words as they are written in the logfile
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();
}
}
The log file itself reads as follows
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
Now I would like to replace each line above with my own custom words
I tried out something like this
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";
}.....
The else if continues but this code enters a Loop of printing a single first line
please any manipulations?
Thanks in advance
Upvotes: 0
Views: 1096
Reputation: 26075
Modify code as follow:
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();
}
....
Upvotes: 1
Reputation: 504
You should not append text to SelectedText but just set its value: Instead of
rtbLog.SelectedText= rtbLog.SelectedText+"Application engine Started";
do
rtbLog.SelectedText= "Application engine Started";
And so on.
One more comment, although irrelevant to the question, try to use the using pattern with streams.
using (StreamReader streamReader = new StreamReader(logpath))
{
...
}
Reading the log file line by line, as the following code does, solves your problem, although is not optimal performance wise.
You could consider processing the whole stream as string at once (as you already did) and pattern matching using regular expressions if the log file format is relatively simple.
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";
}
}
}
Upvotes: 0