1

我正在创建一个对色盲友好的日志阅读器,它将解析默认颜色为蓝色的日志文件(文本文件)。当一行包含“错误:”时,它应该将颜色设置为橙色。听起来很简单,但我编写的代码似乎没有检测到该行包含“错误:”。这是我的代码:

        // Network Log
        string errorText1 = " ERROR: ";
        var i1 = 0;
        // From Bottom To Top
        var lines1 = File.ReadAllLines(Properties.Settings.Default.homeDirectory + @"\network.log").Reverse().Skip(1);

        foreach (string line1 in lines1)
        {
            // Catch ERROR: And Color Orange Else Blue
            if (line1.Contains(errorText1))
            {
                listBox1.ForeColor = Color.FromArgb(230, 159, 0);
                this.listBox1.Items.Add(line1);
            }
            else
            {
                listBox1.ForeColor = Color.FromArgb(0, 114, 178);
                this.listBox1.Items.Add(line1);
            }
            i1++;
            if (i1 >= logLength) break;
        }

感谢您对此的任何帮助..

带有错误的日志行示例:

I20160128:051414 错误:[网络]:连接到错误:weather-station.org:24556

Orel Eraki 让我走上了正确的道路,但我不确定他为什么使用类或命名空间来处理要打印的行。类中将没有任何内容。所以我尝试了以下操作,因为这是 Form_Load 类的一部分:

        // Network Log
        string errorText1 = " ERROR: ";
        var i1 = 0;
        // From Bottom To Top
        var lines1 = File.ReadAllLines(Properties.Settings.Default.homeDirectory + @"\network.log").Reverse().Skip(1);

        foreach (string line1 in lines1)
        {
            var color = Color.FromArgb(0, 114, 178); // Blue
            if (line1.Contains(errorText1))
            {
                color = Color.FromArgb(230, 159, 0); // Orange
            }
            this.listBox1.ForeColor = color; // Apply Color
            this.listBox1.Items.Add(line1);

            i1++;
            if (i1 >= logLength) break;
        }

然而,这也不起作用.. :(

再次感谢集思广益和解决此问题的任何帮助。

更新完整代码如下:

原来的:

using System;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace WWIV5TelnetServer
{
    public partial class LogForm : Form
    {
        public LogForm()
        {
            InitializeComponent();
        }

        private void LogForm_Load(object sender, EventArgs e)
        {
            // Default Number Of Lines Per Log
            int logLength  = Int32.Parse(logLines.Text);

            // Network Log
            string errorText1 = " ERROR: ";
            var i1 = 0;
            // From Bottom To Top
            var lines1 = File.ReadAllLines(Properties.Settings.Default.homeDirectory + @"\network.log").Reverse().Skip(1);

            foreach (string line1 in lines1)
            {
                // Catch ERROR: And Color Orange Else Blue
                if (line1.Contains(errorText1))
                {
                    listBox1.ForeColor = Color.FromArgb(230, 159, 0);
                    this.listBox1.Items.Add(line1);
                }
                else
                {
                    listBox1.ForeColor = Color.FromArgb(0, 114, 178);
                    this.listBox1.Items.Add(line1);
                }
                i1++;
                if (i1 >= logLength) break;
            }

            // Networkb Log
            string errorText2 = " ERROR: ";
            var i2 = 0;
            // From Bottom To Top
            var lines2 = File.ReadAllLines(Properties.Settings.Default.homeDirectory + @"\networkb.log").Reverse().Skip(1);

            foreach (string line2 in lines2)
            {
                this.listBox2.Items.Add(line2);
                i1++;
                if (i2 >= logLength) break;
            }

            foreach (string line2 in lines2)
            {
                // Catch ERROR: And Color Orange Else Blue
                if (line2.Contains(errorText2))
                {
                    listBox2.ForeColor = Color.FromArgb(230, 159, 0);
                    this.listBox2.Items.Add(line2);
                }
                else
                {
                    listBox2.ForeColor = Color.FromArgb(0, 114, 178);
                    this.listBox2.Items.Add(line2);
                }
                i2++;
                if (i2 >= logLength) break;
            }

            // Net.log
            string errorText3 = " ERROR: ";
            var i3 = 0;
            // From Bottom To Top
            var lines3 = File.ReadAllLines(Properties.Settings.Default.homeDirectory + @"\gfiles\NET.LOG");

            foreach (string line3 in lines3)
            {
                // Default Color Blue
                listBox3.ForeColor = Color.FromArgb(0, 114, 178);

                // Catch ERROR: And Color Orange Else Blue
                if (line3.Contains(errorText3))
                {
                    listBox3.ForeColor = Color.FromArgb(230, 159, 0);
                }
                this.listBox3.Items.Add(line3);
                i3++;
                if (i3 >= logLength) break;
            }

            // Change Log
            var lines4 = File.ReadAllLines(Properties.Settings.Default.homeDirectory + @"\changelog.txt");

            foreach (string line4 in lines4)
            {
                // Default Color Black
                listBox4.ForeColor = Color.Black;

                this.listBox4.Items.Add(line4);
            }

            // What's New
            var lines5 = File.ReadAllLines(Properties.Settings.Default.homeDirectory + @"\whatsnew.txt");

            foreach (string line5 in lines5)
            {
                // Default Color Black
                listBox5.ForeColor = Color.Black;

                this.listBox5.Items.Add(line5);
            }
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Refresh();
        }
    }
}

Orel 的概念毫无疑问是我没有显示整个 .cs 的错:

using System;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace WWIV5TelnetServer
{
    public partial class LogForm : Form
    {
        public LogForm()
        {
            InitializeComponent();
        }

        private Brush GetMessageBrush(string message)
        {
            var brush = Brushes.Green; // Default
            if (message.Contains("ERROR:"))
            {
                brush = Brushes.Red;
            }
            return brush;
        }

        private void listBox2_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index > -1)
            {
                string item = listBox2.Items[e.Index].ToString();
                if (item != null)
                {
                    var brush = GetMessageBrush(item);
                    e.Graphics.DrawString(item, e.Font, brush, e.Bounds.X, e.Bounds.Y, StringFormat.GenericDefault);
                }
                e.DrawFocusRectangle();
            }
        }

        private void LogForm_Load(object sender, EventArgs e)
        {
            // Default Number Of Lines Per Log
            int logLength  = Int32.Parse(logLines.Text);

            // Network Log
            string errorText1 = " ERROR: ";
            var i1 = 0;
            // From Bottom To Top
            var lines1 = File.ReadAllLines(Properties.Settings.Default.homeDirectory + @"\network.log").Reverse().Skip(1);

            foreach (string line1 in lines1)
            {
                // Catch ERROR: And Color Orange Else Blue
                if (line1.Contains(errorText1))
                {
                    listBox1.ForeColor = Color.FromArgb(230, 159, 0);
                    this.listBox1.Items.Add(line1);
                }
                else
                {
                    listBox1.ForeColor = Color.FromArgb(0, 114, 178);
                    this.listBox1.Items.Add(line1);
                }
                i1++;
                if (i1 >= logLength) break;
            }

            // Networkb Log
            //string errorText2 = " ERROR: ";
            var i2 = 0;
            // From Bottom To Top
            var lines2 = File.ReadAllLines(Properties.Settings.Default.homeDirectory + @"\networkb.log").Reverse().Skip(1);

            foreach (string line2 in lines2)
            {
                this.listBox2.Items.Add(line2);
                i1++;
                if (i2 >= logLength) break;
            }
            /*foreach (string line2 in lines2)
            {
                var color = Color.FromArgb(0, 114, 178);
                if (line2.Contains(errorText2))
                {
                    color = Color.FromArgb(230, 159, 0);
                }
                this.listBox2.ForeColor = color;
                this.listBox2.Items.Add(line2);

                i2++;
                if (i2 >= logLength) break;
            }
            foreach (string line2 in lines2)
            {
                // Catch ERROR: And Color Orange Else Blue
                if (line2.Contains(errorText2))
                {
                    listBox2.ForeColor = Color.FromArgb(230, 159, 0);
                    this.listBox2.Items.Add(line2);
                }
                else
                {
                    listBox2.ForeColor = Color.FromArgb(0, 114, 178);
                    this.listBox2.Items.Add(line2);
                }
                i2++;
                if (i2 >= logLength) break;
            }*/

            // Net.log
            string errorText3 = " ERROR: ";
            var i3 = 0;
            // From Bottom To Top
            var lines3 = File.ReadAllLines(Properties.Settings.Default.homeDirectory + @"\gfiles\NET.LOG");

            foreach (string line3 in lines3)
            {
                // Default Color Blue
                listBox3.ForeColor = Color.FromArgb(0, 114, 178);

                // Catch ERROR: And Color Orange Else Blue
                if (line3.Contains(errorText3))
                {
                    listBox3.ForeColor = Color.FromArgb(230, 159, 0);
                }
                this.listBox3.Items.Add(line3);
                i3++;
                if (i3 >= logLength) break;
            }

            // Change Log
            var lines4 = File.ReadAllLines(Properties.Settings.Default.homeDirectory + @"\changelog.txt");

            foreach (string line4 in lines4)
            {
                // Default Color Black
                listBox4.ForeColor = Color.Black;

                this.listBox4.Items.Add(line4);
            }

            // What's New
            var lines5 = File.ReadAllLines(Properties.Settings.Default.homeDirectory + @"\whatsnew.txt");

            foreach (string line5 in lines5)
            {
                // Default Color Black
                listBox5.ForeColor = Color.Black;

                this.listBox5.Items.Add(line5);
            }
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Refresh();
        }
    }
}

注意:正如您在原件中看到的那样,我尝试了一个单独的案例来处理颜色编码。让我们只坚持一个日志,这样我就可以在所有需要颜色编码的日志中实现它。(即列表框1)

希望这有助于清理混乱。遗憾的是,绘制对象并不容易实现。

更新:只是试图在一个新项目中使用 Orel 的代码只是为了测试它,因为它正在做同样的尝试将它实现到我的项目中,不会给任何东西着色。都是黑色的文字。

using System;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace TextTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index > -1)
            {
                string item = listBox1.Items[e.Index].ToString();
                if (item != null)
                {
                    var brush = GetMessageBrush(item);
                    e.Graphics.DrawString(item, e.Font, brush, e.Bounds.X, e.Bounds.Y, StringFormat.GenericDefault);
                }
                e.DrawFocusRectangle();
            }
        }

        private Brush GetMessageBrush(string message)
        {
            var brush = Brushes.Green; // Default
            if (message.Contains("ERROR:"))
            {
                brush = Brushes.Red;
            }
            return brush;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //string errorText1 = " ERROR: ";
            int logLength = 1000;
            var i1 = 0;
            // From Bottom To Top
            var lines1 = File.ReadAllLines(@"networkb.log").Reverse().Skip(1);

            foreach (string line in lines1)
            {
                this.listBox1.Items.Add(line);
                i1++;
                if (i1 >= logLength) break;
            }
        }
    }
}
4

1 回答 1

4

默认绘制方法不会为您执行此操作,您需要进行一些调整才能使其正常工作。

脚步:

  1. 将您的 ListBox DrawMode属性标记为OwnerDrawFixed
  2. 将新的DrawItem事件添加到 ListBox并双击它(它将生成正确的事件方法。
  3. 将当前事件生成的代码替换为以下内容

DrawItem 事件处理程序

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    if (e.Index > -1)
    {
        string item = listBox1.Items[e.Index].ToString();
        if (item != null)
        {
            var brush = GetMessageBrush(item);
            e.Graphics.DrawString(item, e.Font, brush, e.Bounds.X, e.Bounds.Y, StringFormat.GenericDefault);
        }
        e.DrawFocusRectangle();
    }
}

还添加以下方法并根据需要自定义它:

private Brush GetMessageBrush(string message)
{
    var brush = Brushes.Green; // Default
    if (message.Contains("ERROR"))
    {
        brush = Brushes.Red;
    }
    return brush;
}

现在您的新添加代码不需要处理项目绘制

foreach (string line in lines1)
{
    this.listBox1.Items.Add(line);
    i1++;
    if (i1 >= logLength) break;
}
于 2016-01-29T19:07:47.097 回答