コード例 #1
0
 static object ConvertOrLookupByName(string valueOrSwitchName, Type type, IReadOnlyDictionary <string, LoggingLevelSwitch> declaredSwitches)
 {
     if (type == typeof(LoggingLevelSwitch))
     {
         return(LookUpSwitchByName(valueOrSwitchName, declaredSwitches));
     }
     return(SettingValueConversions.ConvertToType(valueOrSwitchName, type));
 }
コード例 #2
0
        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);
        }