Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C#
Article

Useful Console Logger Class to Use in All Test Applications

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
27 Sep 2008CPOL 13.9K   116   7  
Util Log class to print timestamp and thread info with each Print

Introduction

With each test application, we would like to print some output. It's pretty useful to have information about time of action and what thread it happened at. Moreover, prompt with waiting for user to press ENTER is common.  

Using the Code

Here is the Log class text:

C#
public class Log
{
    private Log()
    {
    }

    public static void Print(string text, params object[] args)
    {
        DoLog("", text, args);
    }

    public static void Info(string text, params object[] args)
    {
        DoLog("INFO ", text, args);
    }

    public static void Warning(string text, params object[] args)
    {
        DoLog("WARNING ", text, args);
    }
    
    public static void Error(string text, params object[] args)
    {
        DoLog("!!!ERROR!!! ", text, args);
    }

    public static void WaitForEnter()
    {
        WaitForEnter("Press ENTER to continue...");
    }

    public static void WaitForEnter(string prompt)
    {
        DoLog("", prompt);
        Console.ReadLine();
    }

    private static void DoLog(string prefix, string text, params object[] args)
    {
        // For newer .NET Frameworks you should use 
        // "System.Threading.Thread.CurrentThread.ManagedThreadId()"
        // as "AppDomain.GetCurrentThreadId()" is deprecated
        // there. But unfortunately it is not available in .NET v.1.1
        int threadId = AppDomain.GetCurrentThreadId();
        Console.Write("[{0:D4}] [{1}] ",
            threadId,
            DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
        Console.Write(prefix);
        Console.WriteLine(text, args);
    }
}

For such a program: 

C#
class Program
{
    public Program()
    {
        Log.Print("Program()");
    }

    ~Program()
    {
        Log.Print("~Program()");
    }
    
    static void Main(string[] args)
    {
        Program program = new Program();
        Log.Print("Test");
        Log.Error("MyError");
        Log.Print("Test #{0}{1}", 1, ":o)");
        Log.Error("MyError #{0}{1}", 1, ":o)");
        Log.WaitForEnter();
    }
}  

It gives you this output: 

[3276] [2008/09/27 08:52:05.431] Program()
[3276] [2008/09/27 08:52:05.434] Test
[3276] [2008/09/27 08:52:05.434] !!!ERROR!!! MyError
[3276] [2008/09/27 08:52:05.435] Test #1
[3276] [2008/09/27 08:52:05.435] !!!ERROR!!! MyError #1
[3276] [2008/09/27 08:52:05.435] Test #1:o)
[3276] [2008/09/27 08:52:05.435] !!!ERROR!!! MyError #1:o)
[3276] [2008/09/27 08:52:05.435] Press ENTER to continue...

[4500] [2008/09/27 08:52:07.682] ~Program()   

As we can see, our Program object was destroyed from some other thread (e.g. by GC thread). 

History

  • 27th September, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --