Click here to Skip to main content
Licence CPOL
First Posted 27 Sep 2008
Views 4,132
Downloads 43
Bookmarked 7 times

Useful Console Logger Class to Use in All Test Applications

By onxonx | 27 Sep 2008
Util Log class to print timestamp and thread info with each Print

1

2
1 vote, 33.3%
3

4
2 votes, 66.7%
5
4.33/5 - 3 votes
μ 4.33, σa 2.02 [?]

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:

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: 

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)

About the Author

onxonx

Software Developer (Senior)

United States United States

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 27 Sep 2008
Article Copyright 2008 by onxonx
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid