如何从外部程序在c#中创建的日志文件中编写自定义日志到richtextbox

本文关键字:日志 文件 自定义 richtextbox 创建 从外部 程序 | 更新日期: 2023-09-27 18:06:52

我有一个简单的应用程序,让它命名为"X",它使用System.Diagnostic.Process.Application "Y"启动外部程序"Y",同时执行将应用程序日志写入文本文件。

现在我想阅读应用程序"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";
            }.....

else if继续,但是这段代码进入了一个循环,只打印第一行

有什么操作吗?

Thanks in advance

如何从外部程序在c#中创建的日志文件中编写自定义日志到richtextbox

修改代码如下:

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

你不应该在SelectedText中添加文本,而应该设置它的值:而不是

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

rtbLog.SelectedText= "Application engine Started";

等等

还有一个注释,虽然与问题无关,但请尝试对流使用using模式。

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