title: Logging information to a windowed textbox
body: <p>I am working on converting a console application into a windowed format and as someone who knows little about it but has had experience with a similar application already in window format in the past I figured it wouldn't be too hard.</p>
  
  <p>So I created a form and added a textbox to it just to get the logging information to start with.</p>
  
  <p>This console app used to run in a single thread, I have since added a second thread to allow the form to run side by side with the console for testing. (it runs fine in a single thread strangely now too).</p>
  
  <p>This is the code I am using to write text to the form except that I am not getting ANYTHING at all on the form.</p>
  
  <pre><code>    static Form1 f = new Form1();
      delegate void SetTextCallback(string s);
  
      private void SetText(string text)
      {
          // InvokeRequired required compares the thread ID of the 
          // calling thread to the thread ID of the creating thread. 
          // If these threads are different, it returns true. 
          if (f.textBox1.InvokeRequired)
          {
              SetTextCallback d = new SetTextCallback(SetText);
              f.textBox1.Invoke(d, new object[] { text });
          }
          else
          {
              f.textBox1.AppendText(text);
          }
      }
  </code></pre>
  
  <p>I can confirm that there is text entering the "text" variable but it is not getting to the form.</p>
  
  <p>Any help would be appreciated.</p>
  
  <p>This is the full file:</p>
  
  <pre><code>using System;
  using System.Windows.Forms;
  using Chraft.Properties;
  using System.IO;
  using Chraft.Plugins.Events.Args;
  using Chraft.Plugins.Events;
  
  namespace Chraft
  {
  public class Logger
  {
      private StreamWriter WriteLog;
      private Server Server;
  
      internal Logger(Server server, string file)
      {
          Server = server;
          try
          {
              WriteLog = new StreamWriter(file, true);
              WriteLog.AutoFlush = true;
          }
          catch
          {
              WriteLog = null;
          }
      }
  
      ~Logger()
      {
          try
          {
              WriteLog.Close();
          }
          catch
          {
          }
      }
  
      public void Log(LogLevel level, string format, params object[] arguments)
      {
          Log(level, string.Format(format, arguments));
      }
  
      public void Log(LogLevel level, string message)
      {
          //Event
          LoggerEventArgs e = new LoggerEventArgs(this, level, message);
          Server.PluginManager.CallEvent(Event.LOGGER_LOG, e);
          if (e.EventCanceled) return;
          level = e.LogLevel;
          message = e.LogMessage;
          //End Event
  
          LogToConsole(level, message);
          LogToForm(level, message);
          LogToFile(level, message);
      }
  
      private void LogToConsole(LogLevel level, string message)
      {
          if ((int)level &gt;= Settings.Default.LogConsoleLevel)
          {
              Console.WriteLine(Settings.Default.LogConsoleFormat, DateTime.Now, level.ToString().ToUpper(), message);
          }
      }
      static Form1 f = new Form1();
      delegate void SetTextCallback(string s);
  
      private void SetText(string text)
      {
          // InvokeRequired required compares the thread ID of the 
          // calling thread to the thread ID of the creating thread. 
          // If these threads are different, it returns true. 
          if (f.textBox1.InvokeRequired)
          {
              SetTextCallback d = new SetTextCallback(SetText);
              f.textBox1.Invoke(d, new object[] { text });
          }
          else
          {
              f.textBox1.AppendText(text);
          }
      }
  
      private void LogToForm(LogLevel level, string message)
      {
          if ((int)level &gt;= Settings.Default.LogConsoleLevel)
          {
              SetText(DateTime.Now + level.ToString().ToUpper() + message);
          }
      }
  
      private void LogToFile(LogLevel level, string message)
      {
          if ((int)level &gt;= Settings.Default.LogFileLevel &amp;&amp; WriteLog != null)
              WriteLog.WriteLine(Settings.Default.LogFileFormat, DateTime.Now, level.ToString().ToUpper(), message);
      }
  
      public void Log(Exception ex)
      {
          //Event
          LoggerEventArgs e = new LoggerEventArgs(this, LogLevel.Debug, ex.ToString(), ex);
          Server.PluginManager.CallEvent(Event.LOGGER_LOG, e);
          if (e.EventCanceled) return;
          //End Event
  
          Log(LogLevel.Debug, ex.ToString());
      }
  
  
      public enum LogLevel : int
      {
          Trivial = -1,
          Debug = 0,
          Info = 1,
          Warning = 2,
          Caution = 3,
          Notice = 4,
          Error = 5,
          Fatal = 6
      }
  }
  }
  </code></pre>