title: Parsing a Log (text file) with colors in C#
body: <p>I'm creating a log reader that will be Color Blind Friendly that will parse a log file (text file) with the default color of blue. When a line contains "ERROR:" it should set the color to Orange. Sounds simple enough but the code I whipped up does not seem to detect that the line contains "ERROR:" in it. Here is my code:</p>
  
  <pre><code>        // 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 &gt;= logLength) break;
          }
  </code></pre>
  
  <p>Thanks for any help on this.. </p>
  
  <p>Example of a Log line with an Error:</p>
  
  <p>I20160128:051414 ERROR: [network]: Error connecting to: weather-station.org:24556</p>
  
  <p>Orel Eraki got me on the right path it looks like but I'm not sure why he used a Class or Namespace to handle the line to be printed. There would be nothing to put in the Class. So I tried the follow since this is part of the Form_Load Class:</p>
  
  <pre><code>        // 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 &gt;= logLength) break;
          }
  </code></pre>
  
  <p>However this did not work either.. :(</p>
  
  <p>Again thanks for brainstorming and for any help to resolve this issue.</p>
  
  <p>UPDATE FULL CODE AS FOLLOWS:</p>
  
  <p>Original:</p>
  
  <pre><code>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 &gt;= 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 &gt;= 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 &gt;= 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 &gt;= 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();
          }
      }
  }
  </code></pre>
  
  <p>Orel's Concept which is no doubt my fault for not showing the entire .cs:</p>
  
  <pre><code>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 &gt; -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 &gt;= 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 &gt;= 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 &gt;= 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 &gt;= 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 &gt;= 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();
          }
      }
  }
  </code></pre>
  
  <p>NOTES: As you can see in the Original I tried a separate case to handle the color coding. Lets just stick to one log so that I can then implement that across all logs that require the color coding. (ie. listBox1)</p>
  
  <p>Hope this helps clears up the mess. It's a shame that the Draw Object isn't easier to implement.</p>
  
  <p>UPDATE: Just trying to use Orel's code in a new project just to test it because it was doing the same trying to implement it into mine, does not color anything. It's all black text.</p>
  
  <pre><code>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 &gt; -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 &gt;= logLength) break;
              }
          }
      }
  }
  </code></pre>