public ILogger CreateLogger(string categoryName)
        {
            if (categoryName == null)
            {
                throw new ArgumentNullException(nameof(categoryName));
            }

            if (!_loggers.TryGetValue(categoryName, out var logger))
            {
                logger = new DiaggregatorLogger(categoryName);
                if (ReferenceEquals(_loggers.GetOrAdd(categoryName, logger), logger))
                {
                    do
                    {
                        if (!_categoryNames.TryAdd(categoryName, true))
                        {
                            break;
                        }

                        var index = categoryName.LastIndexOf('.');
                        categoryName = index == -1 ? null : categoryName.Substring(0, index);
                    }while (categoryName != null);
                }
            }

            return(logger);
        }
Esempio n. 2
0
        private DiaggregatorLogger[] GetLoggers(string categoryName)
        {
            DiaggregatorLogger[] loggers;
            var categoryNames = _loggerProvider.GetCategoryNames();

            if (string.IsNullOrEmpty(categoryName))
            {
                loggers = new DiaggregatorLogger[categoryNames.Count];
                for (var i = 0; i < categoryNames.Count; i++)
                {
                    loggers[i] = (DiaggregatorLogger)_loggerProvider.CreateLogger(categoryNames[i]);
                }

                return(loggers);
            }


            var start = 0;

            for (; start < categoryNames.Count; start++)
            {
                if (StringComparer.Ordinal.Compare(categoryName, categoryNames[start]) != 0)
                {
                    break;
                }
            }

            var end = start + 1;

            for (; end < categoryNames.Count; end++)
            {
                if (!categoryNames[end].StartsWith(categoryName) ||
                    categoryNames[end].Length <= categoryName.Length &&
                    categoryNames[end][categoryName.Length] != '.')
                {
                    break;
                }
            }

            loggers = new DiaggregatorLogger[end - start];
            for (var i = start; i < end; i++)
            {
                loggers[i - start] = (DiaggregatorLogger)_loggerProvider.CreateLogger(categoryNames[i]);
            }

            return(loggers);
        }