You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.7 KiB

8 months ago
  1. using NLog;
  2. using NLog.Config;
  3. using NLog.Targets;
  4. namespace Mirle.Component.Record
  5. {
  6. /// <summary>
  7. /// 記錄檔類別
  8. /// </summary>
  9. public class LogWrapper
  10. {
  11. /// <summary>
  12. /// 建構式
  13. /// </summary>
  14. private LogWrapper() { }
  15. /// <summary>
  16. /// 關聯式資料庫物件
  17. /// </summary>
  18. private static volatile LogWrapper _logFile;
  19. /// <summary>
  20. /// 物件鎖定
  21. /// </summary>
  22. private static readonly object _syncRoot = new object();
  23. /// <summary>
  24. /// 記錄設定檔
  25. /// </summary>
  26. private static readonly LoggingConfiguration _loggingConfiguration = new LoggingConfiguration();
  27. /// <summary>
  28. /// 紀錄檔實例
  29. /// </summary>
  30. public static LogWrapper Instance
  31. {
  32. get
  33. {
  34. if (_logFile == null)
  35. {
  36. lock (_syncRoot) // 鎖定避免多執行緒重覆呼叫建立物件
  37. {
  38. _logFile = new LogWrapper();
  39. }
  40. }
  41. return _logFile;
  42. }
  43. }
  44. /// <summary>
  45. /// 取得記錄器
  46. /// </summary>
  47. /// <param name="logger">紀錄器名稱</param>
  48. /// <param name="fileTarget">紀錄器設定檔</param>
  49. /// <returns>紀錄器物件</returns>
  50. public Logger GetLogger(string logger, FileTarget fileTarget)
  51. {
  52. _loggingConfiguration.AddRuleForAllLevels(fileTarget, logger);
  53. LogManager.Configuration = _loggingConfiguration;
  54. return LogManager.GetLogger(logger);
  55. }
  56. }
  57. }