private static Func CreateFilter()

in Libraries/src/Amazon.Lambda.Logging.AspNetCore/LambdaLoggerOptions.cs [178:259]


        private static Func<string, LogLevel, bool> CreateFilter(IConfiguration logLevelsSection)
        {
            // Empty section means log everything
            var logLevels = logLevelsSection.GetChildren().ToList();
            if (logLevels.Count == 0)
            {
                return null;
            }

            // Populate mapping of category to LogLevel
            var logLevelsMapping = new Dictionary<string, LogLevel>(StringComparer.Ordinal);
            var defaultLogLevel = LogLevel.Information;
            foreach (var logLevel in logLevels)
            {
                var category = logLevel.Key;
                var minLevelValue = logLevel.Value;
                LogLevel minLevel;
                if (!Enum.TryParse(minLevelValue, out minLevel))
                {
                    throw new InvalidCastException($"Unable to convert level '{minLevelValue}' for category '{category}' to LogLevel.");
                }

                if (category.Contains("*"))
                {
                    // Only 1 wildcard is supported
                    var wildcardCount = category.Count(x => x == '*');
                    if (wildcardCount > 1)
                    {
                        throw new ArgumentOutOfRangeException($"Category '{category}' is invalid - only 1 wildcard is supported in a category.");
                    }

                    // Wildcards are only supported at the end of a Category name!
                    var wildcardLocation = category.IndexOf('*');
                    if (wildcardLocation != category.Length - 1)
                    {
                        throw new ArgumentException($"Category '{category}' is invalid - wilcards are only supported at the end of a category.");
                    }

                    var trimmedCategory = category.TrimEnd('*');
                    logLevelsMapping[trimmedCategory] = minLevel;
                }
                else if (category.Equals(DEFAULT_CATEGORY, StringComparison.OrdinalIgnoreCase))
                {
                    defaultLogLevel = minLevel;
                }
                else
                {
                    logLevelsMapping[category] = minLevel;
                }
            }

            // Extract a reverse sorted list of categories. This allows us to quickly search for most specific match to least specific.
            var orderedCategories = logLevelsMapping.Keys
                .OrderByDescending(categoryKey => categoryKey)
                .ToList();

            // Filter lambda that examines mapping
            return (string category, LogLevel logLevel) =>
            {
                LogLevel minLevel;

                // Exact match takes priority
                if (logLevelsMapping.TryGetValue(category, out minLevel))
                {
                    return logLevel >= minLevel;
                }

                // Find the most specific wildcard or namespace prefix category that matches the logger category
                var matchedCategory = orderedCategories.FirstOrDefault(category.StartsWith);
                if (matchedCategory != null)
                {
                    // If no log filters then default to logging the log message.
                    minLevel = logLevelsMapping[matchedCategory];
                    return logLevel >= minLevel;
                }
                else
                {
                    // If no log filters then default to logging the log message.
                    return (logLevel >= defaultLogLevel);
                }
            };
        }