From ede013b13c9b7572922e70ccbf5fd5dd2cc07248 Mon Sep 17 00:00:00 2001 From: shanj <18996038927@163.com> Date: Thu, 9 Mar 2023 16:10:28 +0800 Subject: [PATCH] 1 --- BBWYB.Common/Log/NLogManager.cs | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 BBWYB.Common/Log/NLogManager.cs diff --git a/BBWYB.Common/Log/NLogManager.cs b/BBWYB.Common/Log/NLogManager.cs new file mode 100644 index 0000000..698aede --- /dev/null +++ b/BBWYB.Common/Log/NLogManager.cs @@ -0,0 +1,34 @@ +using NLog; +using System.Collections.Concurrent; + +namespace BBWYB.Common.Log +{ + public class NLogManager + { + private ConcurrentDictionary loggerDictionary; + private string defaultLoggerName = "default"; + + public NLogManager() + { + loggerDictionary = new ConcurrentDictionary(); + loggerDictionary.TryAdd("default", NLog.LogManager.GetLogger(defaultLoggerName)); + } + + public ILogger Default() + { + return loggerDictionary[defaultLoggerName]; + } + + public ILogger GetLogger(string loggerName) + { + if (string.IsNullOrEmpty(loggerName)) + return Default(); + if (!loggerDictionary.TryGetValue(loggerName, out ILogger logger)) + { + logger = NLog.LogManager.GetLogger(loggerName); + loggerDictionary.TryAdd(loggerName, logger); + } + return logger; + } + } +}