serilog/serilog · error · FormatException

"{switchName}" is not a valid name for a Level Switch declar

Error message

"{switchName}" is not a valid name for a Level Switch declaration. Level switch must be declared with a '$' sign, like "level-switch:$switchName"

What it means

Error ""{switchName}" is not a valid name for a Level Switch declaration. Level switch must be declared with a '$' sign, like "level-switch:$switchName"" thrown in serilog/serilog.

Source

Thrown at src/Serilog/Settings/KeyValuePairs/KeyValuePairSettings.cs:186

        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 (switchInitialLevel == string.Empty)
            {
                newSwitch = new();
            }
            else
            {
                var initialLevel = (LogEventLevel)Enum.Parse(typeof(LogEventLevel), switchInitialLevel);
                newSwitch = new(initialLevel);
            }

            namedSwitches.Add(switchName, newSwitch);
        }
        return namedSwitches;
    }

    static LoggingLevelSwitch LookUpSwitchByName(string switchName, IReadOnlyDictionary<string, LoggingLevelSwitch> declaredLevelSwitches)

View on GitHub (pinned to 49b5339ce8)

When it happens

Trigger: Thrown at src/Serilog/Settings/KeyValuePairs/KeyValuePairSettings.cs:186 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of serilog/serilog@49b5339ce8 (2026-08-04). Data as JSON: /data/errors/5b7b1b153d1ee012.json. Report an issue: GitHub.