Logger.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System.Diagnostics;
  2. namespace WingServerCommon.Log
  3. {
  4. /// <summary>
  5. /// Logger - Should register log engine before usage
  6. /// </summary>
  7. public class Logger
  8. {
  9. private static ILogEngine _logEngine = new DefaultLogEngine();
  10. public static bool Debug { get; set; }
  11. /// <summary>
  12. /// Register a log engine
  13. /// </summary>
  14. /// <param name="logEngine"></param>
  15. public static void RegisterEngine(ILogEngine logEngine)
  16. {
  17. _logEngine = logEngine;
  18. }
  19. /// <summary>
  20. /// Write log with info level
  21. /// </summary>
  22. /// <param name="msg"></param>
  23. public static void WriteLineInfo(string msg)
  24. {
  25. _logEngine.Write(TraceLevel.Info, msg);
  26. }
  27. /// <summary>
  28. /// Write log with warning level
  29. /// </summary>
  30. /// <param name="msg"></param>
  31. public static void WriteLineWarn(string msg)
  32. {
  33. _logEngine.Write(TraceLevel.Warning, msg);
  34. }
  35. /// <summary>
  36. /// Write log with error level
  37. /// </summary>
  38. /// <param name="msg"></param>
  39. public static void WriteLineError(string msg)
  40. {
  41. _logEngine.Write(TraceLevel.Error, msg);
  42. }
  43. /// <summary>
  44. /// Write log with verbose level. In most cases we write verbose log in debug mode
  45. /// </summary>
  46. /// <param name="msg"></param>
  47. public static void WriteLineVerbose(string msg)
  48. {
  49. _logEngine.Write(TraceLevel.Verbose, msg);
  50. }
  51. /// <summary>
  52. /// Write debug log
  53. /// </summary>
  54. /// <param name="msg"></param>
  55. public static void WriteLineDebug(string msg)
  56. {
  57. if(Debug)
  58. {
  59. _logEngine.Write(TraceLevel.Off, msg);
  60. }
  61. }
  62. class DefaultLogEngine : ILogEngine
  63. {
  64. public void Write(TraceLevel level, string msg){
  65. System.Console.WriteLine("Should register customer log engine first.");
  66. }
  67. }
  68. }
  69. }