{"record":{"id":"f5d93615cf05250d","repo":"microsoft/semantic-kernel","slug":"configuration-key-section-key-not-found","errorCode":null,"errorMessage":"Configuration key '{section}:{key}' not found","messagePattern":"Configuration key '(.+?):(.+?)' not found","errorType":"exception","errorClass":"ConfigurationNotFoundException","httpStatus":null,"severity":"critical","filePath":"dotnet/samples/GettingStartedWithAgents/Step07_Telemetry.cs","lineNumber":220,"sourceCode":"    }\n\n    private TracerProvider? GetTracerProvider(bool useApplicationInsights)\n    {\n        // Enable diagnostics.\n        AppContext.SetSwitch(\"Microsoft.SemanticKernel.Experimental.GenAI.EnableOTelDiagnostics\", true);\n\n        var tracerProviderBuilder = Sdk.CreateTracerProviderBuilder()\n            .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(\"Semantic Kernel Agents Tracing Example\"))\n            .AddSource(\"Microsoft.SemanticKernel*\")\n            .AddSource(s_activitySource.Name);\n\n        if (useApplicationInsights)\n        {\n            var connectionString = TestConfiguration.ApplicationInsights.ConnectionString;\n\n            if (string.IsNullOrWhiteSpace(connectionString))\n            {\n                throw new ConfigurationNotFoundException(\n                    nameof(TestConfiguration.ApplicationInsights),\n                    nameof(TestConfiguration.ApplicationInsights.ConnectionString));\n            }\n\n            tracerProviderBuilder.AddAzureMonitorTraceExporter(o => o.ConnectionString = connectionString);\n        }\n        else\n        {\n            tracerProviderBuilder.AddConsoleExporter();\n        }\n\n        return tracerProviderBuilder.Build();\n    }\n\n    private ILoggerFactory GetLoggerFactoryOrDefault(ILoggerFactory? loggerFactory = null) => loggerFactory ?? NullLoggerFactory.Instance;\n\n    private sealed class ApprovalTerminationStrategy : TerminationStrategy\n    {","sourceCodeStart":202,"sourceCodeEnd":238,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/GettingStartedWithAgents/Step07_Telemetry.cs#L202-L238","documentation":"In ConfigureTracing, when useApplicationInsights is true the code reads TestConfiguration.ApplicationInsights.ConnectionString. If the value is null or whitespace, it throws ConfigurationNotFoundException(section, key) — a custom exception whose message is formatted as 'Configuration key \\'{section}:{key}\\' not found'. This guards against starting an Azure Monitor trace exporter with no connection string, which would fail silently or crash deep in the exporter.","triggerScenarios":"Calling ConfigureTracing(useApplicationInsights: true) when the ApplicationInsights:ConnectionString configuration key is absent — i.e., it wasn't set in user secrets, environment variables, or appsettings.json.","commonSituations":"Switching from console exporter to Application Insights exporter without configuring the connection string; CI environment missing the APPINSIGHTS_CONNECTIONSTRING or ApplicationInsights__ConnectionString variable; user secrets not set for the connection string; the TestConfiguration static initialization didn't pick up the section.","solutions":["Set the Application Insights connection string: 'dotnet user-secrets set \"ApplicationInsights:ConnectionString\" \"<your-connection-string>\"'.","Set the environment variable ApplicationInsights__ConnectionString (double underscore for section separator).","If you don't have Application Insights, call ConfigureTracing(useApplicationInsights: false) to use the console exporter instead.","Verify the connection string is copied from the Azure Portal (Application Insights resource > Overview > Connection String), not the instrumentation key."],"exampleFix":"// before\nif (string.IsNullOrWhiteSpace(connectionString))\n{\n    throw new ConfigurationNotFoundException(\n        nameof(TestConfiguration.ApplicationInsights),\n        nameof(TestConfiguration.ApplicationInsights.ConnectionString));\n}\n\n// after — fall back to console exporter with a warning\nif (string.IsNullOrWhiteSpace(connectionString))\n{\n    Console.WriteLine(\"Application Insights connection string not configured; falling back to console exporter.\");\n    tracerProviderBuilder.AddConsoleExporter();\n}\nelse\n{\n    tracerProviderBuilder.AddAzureMonitorTraceExporter(o => o.ConnectionString = connectionString);\n}","handlingStrategy":"validation","validationCode":"// Check the connection string before configuring the exporter\nvar connectionString = config[\"ApplicationInsights:ConnectionString\"];\nif (useApplicationInsights && string.IsNullOrWhiteSpace(connectionString))\n    throw new ConfigurationNotFoundException(\"ApplicationInsights\", \"ConnectionString\");","typeGuard":"bool HasAppInsightsConfig(IConfiguration c) => !string.IsNullOrWhiteSpace(c[\"ApplicationInsights:ConnectionString\"]);","tryCatchPattern":"try { return ConfigureTracing(useApplicationInsights: true); } catch (ConfigurationNotFoundException) { Console.WriteLine(\"App Insights not configured; using console exporter.\"); return ConfigureTracing(useApplicationInsights: false); }","preventionTips":["Set ApplicationInsights:ConnectionString in user secrets or environment before enabling the exporter.","Use the connection string from the Azure Portal (not the instrumentation key).","Fall back to the console exporter when the connection string is absent in development."],"tags":["configuration","application-insights","telemetry","tracing","configuration-not-found","startup"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}