From f8a72f99ce2e337633f54d53065e28857a14c81b Mon Sep 17 00:00:00 2001 From: sanji Date: Mon, 23 Oct 2023 10:15:44 +0800 Subject: [PATCH] 1 --- SiNan.Common/Log/NLogManager.cs | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 SiNan.Common/Log/NLogManager.cs diff --git a/SiNan.Common/Log/NLogManager.cs b/SiNan.Common/Log/NLogManager.cs new file mode 100644 index 0000000..698aede --- /dev/null +++ b/SiNan.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; + } + } +}