如何从C#中的外部程序创建的日志文件中将自定义日志写入richtextbox

时间:2016-09-12 17:15:51

标签: c# logging richtextbox

我有简单的应用程序可以命名为" X"它启动了外部计划" Y"使用System.Diagnostic.Process.Application" Y"执行时将应用程序日志写入文本文件。

现在我想读一下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";
            }.....

其他如果继续,但此代码进入打印单个第一行的循环

请进行任何操纵吗?

提前致谢

2 个答案:

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

答案 1 :(得分:0)

您不应将文字附加到SelectedText,只需设置其值: 而不是

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

DO

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";
            }
        }
    }