C# .NET - how to bind the windows forms out put to rich textbox

Asked By kavitha varma on 30-Jun-12 01:24 AM
Vikram Singh Saini replied to kavitha varma on 30-Jun-12 05:34 AM
When you say windows forms output, what do you exactly mean? Is your window form is generating some specific output or something else.

Please elaborate more about your requirements. That would help us to answer you well.
kavitha varma replied to Vikram Singh Saini on 30-Jun-12 05:42 AM
I want to save all my console statements and errors  in a logger 
Vikram Singh Saini replied to kavitha varma on 30-Jun-12 05:46 AM
For e.g. if your code has lines:

Console.WriteLine("Working on solution");

try
{
   // Error reported here
}
catch(Exception ex)
{
   // This exception should be logged in the richtextbox of window
   // or should it be logged in some .txt file in drive
}

So if it is Console application it wouldn't be able to write details in richtextbox. It can write all in some external file. Do you need solution for logging these lines in some text file?
kavitha varma replied to Vikram Singh Saini on 30-Jun-12 06:57 AM
Yah is it possible to write that to text file
Vikram Singh Saini replied to kavitha varma on 30-Jun-12 07:35 AM
Here is the code for logging exception generated in console application.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

 

namespace ErrorLogging

{

    class Program

    {

      static void Main(string[] args)

      {

        Console.WriteLine("Working solution");

 

        string x = null;

 

        try

        {

          Console.WriteLine(x.ToString());

        }

        catch (Exception exc)

        {

          FileLogger(exc.ToString());

        }

      }

 

      // This code logs into txt file

      public static void FileLogger(string details)

      {

        if (!File.Exists(@"C:\ErrorFile.txt"))

        {

          FileStream fsi = new FileStream(@"C:\ErrorFile.txt",

         FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);

          StreamWriter sw = new StreamWriter(fsi);

 

          sw.WriteLine(details + Environment.NewLine + "==============================================");

 

          sw.Close();

          fsi.Close();

        }

        else

        {

          FileStream fsi = new FileStream(@"C:\ErrorFile.txt",

         FileMode.Append, FileAccess.Write, FileShare.Write);

          StreamWriter sw = new StreamWriter(fsi);

 

          sw.WriteLine(details + Environment.NewLine + "=================================================");

 

          sw.Close();

          fsi.Close();

        }

      }    

    }

}

 


Let us know if it helped or not.