Click here to Skip to main content
13,800,880 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 24 Jul 2013
Licenced CPOL


, 24 Jul 2013
Rate this:
Please Sign up or sign in to vote.
Simple logging library. You can format your text, write it on console, and store it in a file or database.


I created this simple library that you can use to format your text, write it on console and store it in a file or database.


Log Level

  • Trace - Very detailed log messages
  • Debug - Detailed log messages
  • Information - Informational messages
  • Warning - Warning messages
  • Error - Error messages
  • Fatal - Fatal error messages
  • Success - Success messages

Format Patterns

  • %hh - Current hour
  • %mm - Current minute
  • %ss - Current second
  • %MM - Current month
  • %dd - Current day
  • %yy - Current year
  • %class - Class name where WriteLine has been called
  • %method - Method name where WriteLine has been called
  • %text - Line text
  • %loglevel - Log level name
  • %username - Current windows user name
  • %timestamp - Current timestamp
  • %{ConsoleColorName} - Set color for multicolor line

The Library

/// <summary>
/// Format and write the line
/// </summary>
/// <param name="Text">Text to write</param>     
/// <param name="LogLevel">Log level</param>
public void WriteLine(string Text, LogLevel LogLevel)
  //Skip line if MinimumLogLevel is higher than the given LogLevel 
  if (LogLevel >= MinimumLogLevel)
    //Temp variable of the log format
    string FinalString = Format;
    //Set the given LogLevel console color
    Console.ForegroundColor = 
    //Replace all values
    FinalString = ReplaceValues(FinalString, Text, LogLevel);
    //Set ConsoleColor by color parameters
    while (FinalString.IndexOf("%{") > 0)
      int startIndex = FinalString.IndexOf("%{");
      int endIndex = FinalString.IndexOf("}");
      Console.Write(FinalString.Substring(0, startIndex));
      if (LogFileStream != null)
        LogFileStream.Write(FinalString.Substring(0, startIndex));
      string ColorName = FinalString.Substring(startIndex + 2, 
                              endIndex - (startIndex + 2));
      ConsoleColor ConsoleColor;
      if (Enum.TryParse(ColorName, true, out ConsoleColor))
        Console.ForegroundColor = ConsoleColor;
      FinalString = FinalString.Substring(endIndex + 1);

    if (LogFileStream != null)
      //Write Log to file
    if (DatabaseManager != null)
      //Write Log to database
      string FinalQuery = LogQueryFormat;
      FinalQuery = ReplaceValues(FinalQuery, Text, LogLevel);

private string ReplaceValues(string String, string Text, LogLevel LogLevel)
  StackFrame _SF = new StackTrace().GetFrame(2);
  //Current Hour
  String = String.Replace("%hh", DateTime.Now.ToString("hh"));
  String = String.Replace("%mm", DateTime.Now.ToString("mm"));
  String = String.Replace("%ss", DateTime.Now.ToString("ss"));
  String = String.Replace("%MM", DateTime.Now.ToString("MM"));
  String = String.Replace("%dd", DateTime.Now.ToString("dd"));
  String = String.Replace("%yy", DateTime.Now.ToString("yy"));
  //Class name where WriteLine has been called
  String = String.Replace("%class", _SF.GetMethod().ReflectedType.Name);
  //method name where WriteLine has been called
  String = String.Replace("%method", _SF.GetMethod().Name);
  String = String.Replace("%text", Text);
  //LogLevel name
  String = String.Replace("%loglevel", LogLevel.ToString());
  //User login name
  String = String.Replace("%username", Environment.UserName);
  //Current timestamp
  String = String.Replace("%timestamp", Stopwatch.GetTimestamp().ToString());
  return String;

Test Program and How to Use the Library

Example of a Format String

[%loglevel][%dd/%MM/%yy][%hh:%mm:%ss]%{cyan}[%class.%method] > %{red}%text 


static void Main(string[] args)
  //Create a new logging with our custom format
  Logging MyLog = new Logging(
    "[%loglevel][%dd/%MM/%yy][%hh:%mm:%ss]%{cyan}[%class.%method] > %{red}%text");
  //the MinimumLogLevel to display the line
  MyLog.MinimumLogLevel = LogLevel.Debug;
  //Name of a file where logs will be stored
  //Mysql Query Format
  string QueryFormat = "insert into mylog values ('%loglevel','%class','%method','%text')";
  //Mysql Connection parameters
  MyLog.SetLogDatabase("", 3306, 
    "root", "123456", "test", QueryFormat);
  //Write some logs
  MyLog.WriteLine("log", LogLevel.Debug);
  MyLog.WriteLine("information", LogLevel.Information);
  MyLog.WriteLine("warning", LogLevel.Warning);
  MyLog.WriteLine("error", LogLevel.Error);
  MyLog.WriteLine("fatal", LogLevel.Fatal);
  //Change Success LogLevel Default Color
  MyLog.ChangeLogLevelColor(LogLevel.Success, ConsoleColor.DarkGreen); 
  MyLog.WriteLine("success", LogLevel.Success);

  //Create a exception log
  Logging ExceptionLog = new Logging("[%dd/%MM/%yy][%hh:%mm:%ss][%class.%method] > %text");
  ExceptionLog.MinimumLogLevel = LogLevel.Error;
  string QueryFormat2 = 
    "insert into exception_log values ('%timestamp','%class','%method','%text')";
  ExceptionLog.SetLogDatabase("", 3306, "root", 
                              "123456", "test", QueryFormat2);
  ExceptionLog.WriteLine("Exception: ...", LogLevel.Error);         

How to Import the Library

  • Add reference to Logging.Net.dll and MySql.Data.dll
  • Add:
  • using LoggingNet; 
  • Done


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


About the Author

Rogai Lorenzo
Italy Italy

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.181215.1 | Last Updated 24 Jul 2013
Article Copyright 2013 by Rogai Lorenzo
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid