Exemplo n.º 1
0
        private Abstract.Logging.ILogger GetLogger(string name)
        {
            if (_loggerDict.TryGetValue(name, out var 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 var foundLogger))
                {
                    var newLogger = new SerilogLogger(foundLogger.ContextPropertyName,
                                                      foundLogger.SerilogILogger.ForContext(foundLogger.ContextPropertyName, name));
                    _loggerDict.TryAdd(name, newLogger);
                    return(newLogger);
                }
            }

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

            return(null);
        }
Exemplo n.º 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);
 }