Logging.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using log4net.Config;
  2. using log4net.Repository;
  3. using log4net;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace IoTIntegrationPlatform.Common
  11. {
  12. /// <summary>
  13. /// Log帮助类
  14. /// </summary>
  15. public class Logging
  16. {
  17. private static ILoggerRepository repository { get; set; }
  18. private static ILog _log;
  19. private static ILog log
  20. {
  21. get
  22. {
  23. if (_log == null)
  24. {
  25. Configure();
  26. }
  27. return _log;
  28. }
  29. }
  30. /// <summary>
  31. /// 初始化日志
  32. /// </summary>
  33. /// <param name="repositoryName"></param>
  34. /// <param name="configFile"></param>
  35. public static void Configure(string repositoryName = "NETCoreRepository", string configFile = "Log4net.config")
  36. {
  37. repository = LogManager.CreateRepository(repositoryName);
  38. XmlConfigurator.Configure(repository, new FileInfo(configFile));
  39. _log = LogManager.GetLogger(repositoryName, "RollingLogFileAppender");
  40. }
  41. public static void Info(string msg)
  42. {
  43. log.Info(msg);
  44. }
  45. public static void Warn(string msg)
  46. {
  47. log.Warn(msg);
  48. }
  49. public static void Error(Exception exception = null, string msg = "")
  50. {
  51. log.Error(msg, exception);
  52. }
  53. public static void Debug(string msg)
  54. {
  55. log.Debug(msg);
  56. }
  57. public static void Fatal(string msg)
  58. {
  59. log.Fatal(msg);
  60. }
  61. public static void Info(string msg, string name)
  62. {
  63. msg += name + "/n";
  64. log.Info(msg);
  65. }
  66. public static void Error(Exception exception = null, string msg = "", string name = "")
  67. {
  68. msg += name + "/n";
  69. log.Error(msg, exception);
  70. }
  71. }
  72. }