Simple Console Logger - CodeProject

:

Introduction

This is just a simple Logger for your console APP.

Background

I was looking around and could not find a simple class for logging to the console so I slapped this together.

Using the Code

/*
 * CreatedBy: JosephPhilbert}
 * User: jphilbert
 * Date: 3/27/2015
 * Time: 8:16 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Threading;

namespace ownsmtp.logging
{
    static class Logger
    {
        
    }
    
    public interface ServerLogger
    {
        ServerLogLevel LogLevel { get; }

        void Debug(string text, params object[] args);
        void Info(string text, params object[] args);
        void Warn(string text, params object[] args);
        void Error(string text, params object[] args);
        void Error(Exception ex);
        void Error(Exception ex, string text, params object[] args);
    }
    
    public enum ServerLogLevel
    {
        Debug = 4,
        Info = 3,
        Warn = 2,
        Error = 1,
        None = 0
    }
    public class ConsoleLogger : ServerLogger
    {
            
        public ServerLogLevel LogLevel { get; set; }

        public ConsoleLogger(ServerLogLevel logLevel)
        {
            LogLevel = logLevel;
        }

        public void Info(string text, params object[] args)
        {
            if (LogLevel < ServerLogLevel.Info)
                return;
            Console.ForegroundColor = ConsoleColor.Cyan;
            Conlog("(I) ", text, args);
        }
      
        public void Warn(string text, params object[] args)
        {
            
            if (LogLevel < ServerLogLevel.Warn)
                return;
            Console.ForegroundColor = ConsoleColor.Green;
            Conlog("(W) ", text, args);
        }
    
        public void Error(Exception ex)
        {
            
            if (LogLevel < ServerLogLevel.Error)
                return;
            Console.ForegroundColor = ConsoleColor.Red;
            Conlog("(E) ", ex.ToString());
        }
      
        public void Error(string text, params object[] args)
        {
            
            if (LogLevel < ServerLogLevel.Error)
                return;
            Console.ForegroundColor = ConsoleColor.Red;
            Conlog("(E) ", text, args);
        }
            
        public void Error(Exception ex, string text, params object[] args)
        {                
            
            if (LogLevel < ServerLogLevel.Error)
                return;
            Console.ForegroundColor = ConsoleColor.Red;
            Conlog("(E) ", text, args);
        }
        
        public void Debug(string text, params object[] args)
        {
            
            if (LogLevel < ServerLogLevel.Debug)
                return;
            Conlog("(D) ", text, args);
        }

        private static void Conlog(string prefix, string text, params object[] args)
        {
//            If you want to add unique thread identifier
//            int threadId = Thread.CurrentThread.ManagedThreadId;
//            Console.Write("[{0:D4}] [{1}] ", threadId, DateTime.Now.ToString("HH:mm:ss.ffff"));
            Console.Write(DateTime.Now.ToString("HH:mm:ss.ffff"));
            Console.Write(prefix);
            Console.WriteLine(text, args);
            Console.ResetColor();
        }
    }
}

This is how you enable it in your console app (can  be private or public).

Pay attention to the loglevels ... and you can change and add to suit your needs ...
For example if you do "ServerLogLevel.Info" all debug Logs will be ignored.
If all you want to see is errors then you would do "Server.LogLevel.Error" simple
        Debug = 4,
        Info = 3,
        Warn = 2,
        Error = 1,
        None = 0

private static ServerLogger mylogger = new ConsoleLogger(ServerLogLevel.Debug);

Somewhere in your code you do ....

mylogger.Info("This is just for information");

Points of Interest

I am pretty sure I will be adding to this the ability to create logfiles, but for now, it does what I want beautifully.