diff --git a/SBF.Common/Log/NLogManager.cs b/SBF.Common/Log/NLogManager.cs new file mode 100644 index 0000000..7558afc --- /dev/null +++ b/SBF.Common/Log/NLogManager.cs @@ -0,0 +1,34 @@ +using NLog; +using System.Collections.Concurrent; + +namespace SBF.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; + } + } +}