使用C#中的颜色解析日志(文本文件)

时间:2016-01-29 19:03:02

标签: c# winforms visual-studio file.readalllines

我正在创建一个日志阅读器,它将是Color Blind Friendly,它将使用默认颜色blue解析日志文件(文本文件)。当一行包含“ERROR:”时,应将颜色设置为橙色。听起来很简单但是我掀起的代码似乎没有检测到该行包含“ERROR:”。这是我的代码:

        // 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错误:[network]:连接错误:weather-station.org:24556

Orel Eraki让我走上了正确的道路,但我不确定为什么他会使用Class或Namespace来处理要打印的行。班级里没有什么可以放的。所以我尝试了以下内容,因为这是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();
        }
    }
}

奥廖尔的概念毫无疑问是我没有展示整个.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();
        }
    }
}

注意:正如你在原版中看到的那样,我尝试了一个单独的案例来处理颜色编码。让我们只关注一个日志,以便我可以在需要颜色编码的所有日志中实现这一点。 (即listBox1)

希望这有助于清除混乱局面。遗憾的是Draw对象不容易实现。

更新:只是尝试在一个新项目中使用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;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:4)

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

步骤:

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

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