Introduction
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.
Screenshots
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
public void WriteLine(string Text, LogLevel LogLevel)
{
if (LogLevel >= MinimumLogLevel)
{
string FinalString = Format;
Console.ForegroundColor =
GetLogLevelInfoByLogLevel(LogLevel).LogLevelColor;
FinalString = ReplaceValues(FinalString, Text, LogLevel);
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);
}
Console.WriteLine(FinalString);
if (LogFileStream != null)
{
LogFileStream.WriteLine(FinalString);
LogFileStream.Flush();
}
if (DatabaseManager != null)
{
string FinalQuery = LogQueryFormat;
FinalQuery = ReplaceValues(FinalQuery, Text, LogLevel);
DatabaseManager.GetClient().ExecuteQuery(FinalQuery);
}
}
}
private string ReplaceValues(string String, string Text, LogLevel LogLevel)
{
StackFrame _SF = new StackTrace().GetFrame(2);
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"));
String = String.Replace("%class", _SF.GetMethod().ReflectedType.Name);
String = String.Replace("%method", _SF.GetMethod().Name);
String = String.Replace("%text", Text);
String = String.Replace("%loglevel", LogLevel.ToString());
String = String.Replace("%username", Environment.UserName);
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
Code
static void Main(string[] args)
{
Logging MyLog = new Logging(
"[%loglevel][%dd/%MM/%yy][%hh:%mm:%ss]%{cyan}[%class.%method] > %{red}%text");
MyLog.MinimumLogLevel = LogLevel.Debug;
MyLog.SetLogFile("mylog.txt");
string QueryFormat = "insert into mylog values ('%loglevel','%class','%method','%text')";
MyLog.SetLogDatabase("127.0.0.1", 3306,
"root", "123456", "test", QueryFormat);
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);
MyLog.ChangeLogLevelColor(LogLevel.Success, ConsoleColor.DarkGreen);
MyLog.WriteLine("success", LogLevel.Success);
Logging ExceptionLog = new Logging("[%dd/%MM/%yy][%hh:%mm:%ss][%class.%method] > %text");
ExceptionLog.MinimumLogLevel = LogLevel.Error;
ExceptionLog.SetLogFile("exceptionlog.txt");
string QueryFormat2 =
"insert into exception_log values ('%timestamp','%class','%method','%text')";
ExceptionLog.SetLogDatabase("127.0.0.1", 3306, "root",
"123456", "test", QueryFormat2);
ExceptionLog.WriteLine("Exception: ...", LogLevel.Error);
Console.ReadLine();
}
How to Import the Library
- Add reference to Logging.Net.dll and MySql.Data.dll
- Add:
using LoggingNet;
Done