{"id":"5b7b1b153d1ee012","repo":"serilog/serilog","slug":"switchname-is-not-a-valid-name-for-a-level-swi","errorCode":null,"errorMessage":"\"{switchName}\" is not a valid name for a Level Switch declaration. Level switch must be declared with a '$' sign, like \"level-switch:$switchName\"","messagePattern":"\"(.+?)\" is not a valid name for a Level Switch declaration\\. Level switch must be declared with a '\\$' sign, like \"level-switch:\\$switchName\"","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Serilog/Settings/KeyValuePairs/KeyValuePairSettings.cs","lineNumber":186,"sourceCode":"\n        var switchDeclarationDirectives = (from wt in directives\n                                           where matchLevelSwitchDeclarations.IsMatch(wt.Key)\n                                           let match = matchLevelSwitchDeclarations.Match(wt.Key)\n                                           select new\n                                           {\n                                               SwitchName = match.Groups[\"switchName\"].Value,\n                                               InitialSwitchLevel = wt.Value\n                                           }).ToList();\n\n        var namedSwitches = new Dictionary<string, LoggingLevelSwitch>();\n        foreach (var switchDeclarationDirective in switchDeclarationDirectives)\n        {\n            var switchName = switchDeclarationDirective.SwitchName;\n            var switchInitialLevel = switchDeclarationDirective.InitialSwitchLevel;\n            // switchName must be something like $switch to avoid ambiguities\n            if (!IsValidSwitchName(switchName))\n            {\n                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\\\"\");\n            }\n            LoggingLevelSwitch newSwitch;\n            if (switchInitialLevel == string.Empty)\n            {\n                newSwitch = new();\n            }\n            else\n            {\n                var initialLevel = (LogEventLevel)Enum.Parse(typeof(LogEventLevel), switchInitialLevel);\n                newSwitch = new(initialLevel);\n            }\n\n            namedSwitches.Add(switchName, newSwitch);\n        }\n        return namedSwitches;\n    }\n\n    static LoggingLevelSwitch LookUpSwitchByName(string switchName, IReadOnlyDictionary<string, LoggingLevelSwitch> declaredLevelSwitches)","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/serilog/serilog/blob/49b5339ce85385dc52d4d8e8f2b8308becf23506/src/Serilog/Settings/KeyValuePairs/KeyValuePairSettings.cs#L168-L204","documentation":"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.","triggerScenarios":"Thrown at src/Serilog/Settings/KeyValuePairs/KeyValuePairSettings.cs:186 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"analyzedSha":"49b5339ce85385dc52d4d8e8f2b8308becf23506","analyzedAt":"2026-08-04T15:52:35.488Z","schemaVersion":2}