123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- using System;
- namespace SongBrowser.Logging
- {
- public enum LogLevel
- {
- Trace,
- Debug,
- Info,
- Warn,
- Error
- }
- public class Logger
- {
- private static readonly string LoggerName = "SongBrowser";
- private static readonly LogLevel LogLevel = LogLevel.Debug;
- private static readonly ConsoleColor DefaultFgColor = ConsoleColor.Gray;
- private static void ResetForegroundColor()
- {
- Console.ForegroundColor = DefaultFgColor;
- }
- public static void Trace(string format, params object[] args)
- {
- if (LogLevel > LogLevel.Trace)
- {
- return;
- }
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + " - Trace] " + String.Format(format, args));
- ResetForegroundColor();
- }
- public static void Debug(string format, params object[] args)
- {
- if (LogLevel > LogLevel.Debug)
- {
- return;
- }
- Console.ForegroundColor = ConsoleColor.Magenta;
- Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + " - Debug] " + String.Format(format, args));
- ResetForegroundColor();
- }
- public static void Info(string format, params object[] args)
- {
- if (LogLevel > LogLevel.Info)
- {
- return;
- }
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + " - Info] " + String.Format(format, args));
- ResetForegroundColor();
- }
- public static void Log(string format, params object[] args)
- {
- Logger.Debug(format, args);
- }
- public static void Warning(string format, params object[] args)
- {
- if (LogLevel > LogLevel.Warn)
- {
- return;
- }
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + " - Warning] " + String.Format(format, args));
- ResetForegroundColor();
- }
- public static void Error(Exception e)
- {
- Logger.Error("{0}", e.Message);
- }
- public static void Error(string format, params object[] args)
- {
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + " - Error] " + String.Format(format, args));
- ResetForegroundColor();
- }
- public static void Exception(string message)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + "] " + String.Format("{0}", message));
- ResetForegroundColor();
- }
- public static void Exception(Exception e)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + "] " + String.Format("{0}", e));
- ResetForegroundColor();
- }
- public static void Exception(string message, Exception e)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("[" + LoggerName + " @ " + DateTime.Now.ToString("HH:mm") + "] " + String.Format("{0}-{1}-{2}\n{3}", message, e.GetType().FullName, e.Message, e.StackTrace));
- ResetForegroundColor();
- }
- }
- }
|