static object ConvertOrLookupByName(string valueOrSwitchName, Type type, IReadOnlyDictionary <string, LoggingLevelSwitch> declaredSwitches) { if (type == typeof(LoggingLevelSwitch)) { return(LookUpSwitchByName(valueOrSwitchName, declaredSwitches)); } return(SettingValueConversions.ConvertToType(valueOrSwitchName, type)); }
static IReadOnlyDictionary <string, LoggingLevelSwitch> ParseNamedLevelSwitchDeclarationDirectives(IReadOnlyDictionary <string, string> directives) { var matchLevelSwitchDeclarations = new Regex(LevelSwitchDeclarationDirectiveRegex); var switchDeclarationDirectives = (from wt in directives where matchLevelSwitchDeclarations.IsMatch(wt.Key) let match = matchLevelSwitchDeclarations.Match(wt.Key) select new { SwitchName = match.Groups["switchName"].Value, InitialSwitchLevel = wt.Value }).ToList(); var namedSwitches = new Dictionary <string, LoggingLevelSwitch>(); foreach (var switchDeclarationDirective in switchDeclarationDirectives) { var switchName = switchDeclarationDirective.SwitchName; var switchInitialLevel = switchDeclarationDirective.InitialSwitchLevel; // switchName must be something like $switch to avoid ambiguities if (!IsValidSwitchName(switchName)) { throw new FormatException($"\"{switchName}\" is not a valid name for a Level Switch declaration. Level switch must be declared with a '$' sign, like \"level-switch:$switchName\""); } LoggingLevelSwitch newSwitch; if (string.IsNullOrEmpty(switchInitialLevel)) { newSwitch = new LoggingLevelSwitch(); } else { var initialLevel = (LogEventLevel)SettingValueConversions.ConvertToType(switchInitialLevel, typeof(LogEventLevel)); newSwitch = new LoggingLevelSwitch(initialLevel); } namedSwitches.Add(switchName, newSwitch); } return(namedSwitches); }
static void ApplyDirectives(List <IGrouping <string, ConfigurationMethodCall> > directives, IList <MethodInfo> configurationMethods, object loggerConfigMethod) { foreach (var directiveInfo in directives) { var target = SelectConfigurationMethod(configurationMethods, directiveInfo.Key, directiveInfo); if (target != null) { var call = (from p in target.GetParameters().Skip(1) let directive = directiveInfo.FirstOrDefault(s => s.ArgumentName == p.Name) select directive == null ? p.DefaultValue : SettingValueConversions.ConvertToType(directive.Value, p.ParameterType)).ToList(); call.Insert(0, loggerConfigMethod); target.Invoke(null, call.ToArray()); } } }