Logger.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using NTMiner.Impl;
  2. using System;
  3. using System.IO;
  4. namespace NTMiner {
  5. public static class Logger {
  6. /// <summary>
  7. /// 如果未通过<see cref="SetDir(string)"/>更改过则其是程序所在目录下的logs目录。
  8. /// </summary>
  9. public static string DirFullPath { get; private set; } = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs");
  10. public static void SetDir(string fullPath) {
  11. DirFullPath = fullPath;
  12. }
  13. private static bool _isEnabled = true;
  14. public static void Enable() {
  15. _isEnabled = true;
  16. }
  17. /// <summary>
  18. /// 禁用Logger则可以避免行走到Logger逻辑中去,从而避免创建出log文件夹和文件
  19. /// </summary>
  20. public static void Disable() {
  21. _isEnabled = false;
  22. }
  23. // 因为初始化Log4NetLoggingService时会用到Dir所以需要延迟初始化避免静态构造失败
  24. private static readonly Lazy<ILoggingService> _logger = new Lazy<ILoggingService>(() => new Log4NetLoggingService());
  25. public static void Debug(object message) {
  26. if (!_isEnabled) {
  27. return;
  28. }
  29. _logger.Value.Debug(message);
  30. }
  31. public static void InfoDebugLine(object message) {
  32. if (!_isEnabled) {
  33. return;
  34. }
  35. _logger.Value.InfoDebugLine(message);
  36. }
  37. public static void OkDebugLine(object message) {
  38. if (!_isEnabled) {
  39. return;
  40. }
  41. _logger.Value.OkDebugLine(message);
  42. }
  43. public static void WarnDebugLine(object message) {
  44. if (!_isEnabled) {
  45. return;
  46. }
  47. _logger.Value.WarnDebugLine(message);
  48. }
  49. public static void ErrorDebugLine(object message) {
  50. if (!_isEnabled) {
  51. return;
  52. }
  53. _logger.Value.ErrorDebugLine(message);
  54. }
  55. public static void ErrorDebugLine(Exception exception) {
  56. if (!_isEnabled) {
  57. return;
  58. }
  59. if (exception == null) {
  60. return;
  61. }
  62. ErrorDebugLine(exception.GetInnerMessage(), exception);
  63. }
  64. public static void ErrorDebugLine(object message, Exception exception) {
  65. if (!_isEnabled) {
  66. return;
  67. }
  68. _logger.Value.ErrorDebugLine(message, exception);
  69. }
  70. public static void OkWriteLine(object message) {
  71. if (!_isEnabled) {
  72. return;
  73. }
  74. _logger.Value.OkUserLine(message);
  75. }
  76. public static void WarnWriteLine(object message) {
  77. if (!_isEnabled) {
  78. return;
  79. }
  80. _logger.Value.WarnUserLine(message);
  81. }
  82. public static void ErrorWriteLine(object message) {
  83. if (!_isEnabled) {
  84. return;
  85. }
  86. _logger.Value.ErrorUserLine(message);
  87. }
  88. }
  89. }