예제 #1
0
        private Logging.ILogger GetLogger(string name)
        {
            if (_loggerDict.TryGetValue(name, out SerilogLogger logger))
            {
                return(logger);
            }

            string foundLoggerName = null;

            foreach (var loggerName in _loggerDict.Keys)
            {
                if (name.StartsWith(loggerName))
                {
                    if (foundLoggerName == null)
                    {
                        foundLoggerName = loggerName;
                    }
                    else if (loggerName.Length > foundLoggerName.Length)
                    {
                        foundLoggerName = loggerName;
                    }
                }
            }

            if (foundLoggerName != null)
            {
                if (_loggerDict.TryGetValue(foundLoggerName, out SerilogLogger foundLogger))
                {
                    var newLogger = new SerilogLogger(foundLogger.ContextPropertyName, foundLogger.SerilogILogger.ForContext(foundLogger.ContextPropertyName, name));
                    _loggerDict.TryAdd(name, newLogger);
                    return(newLogger);
                }
            }

            if (_loggerDict.TryGetValue(_defaultLoggerName, out SerilogLogger defaultLogger))
            {
                var newLogger = new SerilogLogger(defaultLogger.ContextPropertyName, defaultLogger.SerilogILogger.ForContext(defaultLogger.ContextPropertyName, name));
                _loggerDict.TryAdd(name, newLogger);
                return(newLogger);
            }

            return(null);
        }
예제 #2
0
 /// <summary>Add a file logger.
 /// </summary>
 /// <param name="loggerName"></param>
 /// <param name="logger"></param>
 /// <returns></returns>
 public SerilogLoggerFactory AddFileLogger(string loggerName, SerilogLogger logger)
 {
     _loggerDict.TryAdd(loggerName, logger);
     return(this);
 }