Tuesday, January 11, 2011

Realtime log tracing with log4net using System.Console

I have a project that requires logging information to a text file (and possibly email at a later date).  I decided to look into log4net as I heard many praises about the library.  Within 15 minutes of reading the following tutorial by Tim Corey at CodeProject.com, I was able to start using the tool.  Tim's article is a great "Quick Start" tutorial to get you up and running without fail.
http://www.codeproject.com/KB/dotnet/Log4net_Tutorial.aspx

There was one problem I found while fiddling with the project.  When an error was logged, I needed to find and open the log file to view the error.  What I needed was realtime output of the errors on the console.  Tim, in his article shows how to log to the Output window using a ConsoleAppender, but he does not discuss how to view the errors in a console window for easy and obvious reading (nor does the log4net docs tell you how to do this - not that I found anyway).

I knew that there was a way to redirect output from the Output window (Output/Trace/Debug Window) to the System.Console.  So I had to dust off a book from my bookshelf to come up with the an extremely simple way to provide realtime errors in your console applications.  The code boils down to 2 lines shown here:
TextWriterTraceListener myWriter = new TextWriterTraceListener(System.Console.Out);
Debug.Listeners.Add(myWriter);


To make those lines work with log4net, it is required that you set up your app.config with a TraceAppender as follows:

  <log4net>
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="TraceAppender"/>
    </root>
    <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5level %logger - %message%newline" />
      </layout>
    </appender>
  </log4net>


That's all there is to getting realtime logging info in you console apps without the need to open log files or query your IDE.  I find it useful for testing libraries that will eventually be included in a windows service. 

Below is some sample code. 
public class TestWritingLogToConsole
    {
        private static readonly log4net.ILog log 
= log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public TestWritingLogToConsole()
        {
            log.Info("You should see this text in a System.Console window.");
        }
    }
 
    public class Program
    {
        static void Main(string[] args)
        {
            TextWriterTraceListener myWriter = new TextWriterTraceListener(System.Console.Out);
             Debug.Listeners.Add(myWriter);
 
            System.Diagnostics.Trace.WriteLine("Trace Started.");
            TestWritingLogToConsole test = new TestWritingLogToConsole();
            Console.WriteLine("End Program!");
            Console.Read();
            
        }
    }

Sunday, January 9, 2011

Virtual PC - Using Hard Disk 2

When you add a second hard disk to your Virtual PC, you may notice that the drive does not show in My Computer.  The reason the drive is not recognized is because the volume has not been formatted.  To format the drive open up the Computer Management console (Start >> Control Panel >> Administrative Tools >> Computer Management).  When you click on Disk Management in the tree on the left, a wizard will pop up.  Follow the instructions, noting that the new drive is called Disk 1 (the C: drive is Disk 0).  Once the wizard is complete, right click on the new partition and select Add New Volume.  Follow the instructions shown in the wizard and your second hard drive will be ready to use.

Saturday, January 8, 2011

Reactive Extensions

I've been researching Reactive Extensions and have found that the best tutorial I've encountered so far is at the site listed below.  The "hands on lab" provides you with a sound foundation on which to build/design your own ideas.  Here is the link
http://blogs.msdn.com/b/rxteam/archive/2010/07/15/rx-hands-on-labs-published.aspx

Search Google for "Reactive Extensions hands on lab" if the link above expires.