public void Select(LoggerFilterOptions options, Type providerType, string category, out LogLevel?minLevel, out Func <string, string, LogLevel, bool> filter) { filter = null; minLevel = options.MinLevel; // Filter rule selection: // 1. Select rules for current logger type, if there is none, select ones without logger type specified // 2. Select rules with longest matching categories // 3. If there nothing matched by category take all rules without category // 3. If there is only one rule use it's level and filter // 4. If there are multiple rules use last // 5. If there are no applicable rules use global minimal level var providerAlias = ProviderAliasUtilities.GetAlias(providerType); LoggerFilterRule current = null; foreach (var rule in options.Rules) { if (IsBetter(rule, current, providerType.FullName, category) || (!string.IsNullOrEmpty(providerAlias) && IsBetter(rule, current, providerAlias, category))) { current = rule; } } if (current != null) { filter = current.Filter; minLevel = current.LogLevel; } }
private static LoggerFilterOptions GetLoggerFilterOptions(IConfiguration configuration) { const string logLevelKey = "LogLevel"; var options = new LoggerFilterOptions(); foreach (var configurationSection in configuration.GetChildren()) { if (configurationSection.Key.Equals(logLevelKey, OrdinalIgnoreCase)) { LoadRules(options, configurationSection, default); } else { var logLevelSection = configurationSection.GetSection(logLevelKey); if (logLevelSection == default) { continue; } LoadRules(options, logLevelSection, configurationSection.Key); } } return(options); }
private static LoggerFilterOptions AddRule(LoggerFilterOptions options, string type = null, string category = null, LogLevel?level = null, Func <string, string, LogLevel, bool> filter = null) { options.Rules.Add(new LoggerFilterRule(type, category, level, filter)); return(options); }
private void RefreshFilters(LoggerFilterOptions filterOptions) { lock (_sync) { _filterOptions = filterOptions; foreach (KeyValuePair <string, Logger> registeredLogger in _loggers) { Logger logger = registeredLogger.Value; (logger.MessageLoggers, logger.ScopeLoggers) = ApplyFilters(logger.Loggers); } } }
public LoggerFilterOptions(Microsoft.Extensions.Logging.LoggerFilterOptions options) { this.MinLevel = LogLevel.Information; this.MiniType = LogType.None; foreach (LoggerFilterRule rule in options.Rules) { if (rule.CategoryName.Equals(LoggerSettings.DefaultName)) { this.MiniType = rule.LogType; this.MinLevel = rule.LogLevel ?? LogLevel.Information; } this.Rules.Add(rule); } }
private void RefreshFilters(LoggerFilterOptions filterOptions) { lock (_sync) { _filterOptions = filterOptions; foreach (var logger in _loggers) { var loggerInformation = logger.Value.Loggers; var categoryName = logger.Key; ApplyRules(loggerInformation, categoryName, 0, loggerInformation.Length); } } }
private static void LoadRules(LoggerFilterOptions options, IConfiguration configuration, string logger) { const string defaultCategory = "Default"; foreach (var section in configuration.AsEnumerable(true)) { if (!TryGetSwitch(section.Value, out var level)) { continue; } options.Rules.Add(section.Key.Equals(defaultCategory, OrdinalIgnoreCase) ? new LoggerFilterRule(logger, null, level, null) : new LoggerFilterRule(logger, section.Key, level, null)); } }
/// <summary> /// Creates a new <see cref="LoggerFactory"/> instance. /// </summary> /// <param name="providers">The providers to use in producing <see cref="ILogger"/> instances.</param> /// <param name="filterOptions">The filter options to use.</param> public LoggerFactory(IEnumerable <ILoggerProvider> providers, LoggerFilterOptions filterOptions) : this(providers, new StaticFilterOptionsMonitor(filterOptions)) { }
/// <summary> /// Adds a log filter for the given <see cref="ILoggerProvider"/>. /// </summary> /// <param name="builder">The <see cref="ILoggingBuilder"/> to add the filter to.</param> /// <param name="category">The category to filter.</param> /// <param name="levelFilter">The filter function to apply.</param> /// <typeparam name="T">The <see cref="ILoggerProvider"/> which this filter will be added for.</typeparam> /// <returns>The <see cref="ILoggingBuilder"/> so that additional calls can be chained.</returns> public static LoggerFilterOptions AddFilter <T>(this LoggerFilterOptions builder, string category, Func <LogLevel, bool> levelFilter) where T : ILoggerProvider => AddRule(builder, type : typeof(T).FullName, category : category, filter : (type, name, level) => levelFilter(level));
/// <summary> /// Adds a log filter to the factory. /// </summary> /// <param name="builder">The <see cref="LoggerFilterOptions"/> to add the filter to.</param> /// <param name="category">The category to filter.</param> /// <param name="levelFilter">The filter function to apply.</param> /// <returns>The <see cref="LoggerFilterOptions"/> so that additional calls can be chained.</returns> public static LoggerFilterOptions AddFilter(this LoggerFilterOptions builder, string category, Func <LogLevel, bool> levelFilter) => AddRule(builder, category: category, filter: (type, name, level) => levelFilter(level));
/// <summary> /// Adds a log filter for the given <see cref="ILoggerProvider"/>. /// </summary> /// <param name="builder">The <see cref="LoggerFilterOptions"/> to add the filter to.</param> /// <param name="category">The category to filter.</param> /// <param name="level">The level to filter.</param> /// <typeparam name="T">The <see cref="ILoggerProvider"/> which this filter will be added for.</typeparam> /// <returns>The <see cref="LoggerFilterOptions"/> so that additional calls can be chained.</returns> public static LoggerFilterOptions AddFilter <T>(this LoggerFilterOptions builder, string category, LogLevel level) where T : ILoggerProvider => AddRule(builder, type : typeof(T).FullName, category : category, level : level);
/// <summary> /// Adds a log filter to the factory. /// </summary> /// <param name="builder">The <see cref="LoggerFilterOptions"/> to add the filter to.</param> /// <param name="category">The category to filter.</param> /// <param name="level">The level to filter.</param> /// <returns>The <see cref="LoggerFilterOptions"/> so that additional calls can be chained.</returns> public static LoggerFilterOptions AddFilter(this LoggerFilterOptions builder, string category, LogLevel level) => AddRule(builder, category: category, level: level);
/// <summary> /// Adds a log filter to the factory. /// </summary> /// <param name="builder">The <see cref="LoggerFilterOptions"/> to add the filter to.</param> /// <param name="categoryLevelFilter">The filter function to apply.</param> /// <returns>The <see cref="LoggerFilterOptions"/> so that additional calls can be chained.</returns> public static LoggerFilterOptions AddFilter(this LoggerFilterOptions builder, Func <string, LogLevel, bool> categoryLevelFilter) => AddRule(builder, filter: (type, name, level) => categoryLevelFilter(name, level));
/// <summary> /// Adds a log filter to the factory. /// </summary> /// <param name="builder">The <see cref="ILoggingBuilder"/> to add the filter to.</param> /// <param name="filter">The filter function to apply.</param> /// <returns>The <see cref="ILoggingBuilder"/> so that additional calls can be chained.</returns> public static LoggerFilterOptions AddFilter(this LoggerFilterOptions builder, Func <string, string, LogLevel, bool> filter) => AddRule(builder, filter: filter);