This project reads analysis settings from a JSON file with a cognitive root section. For a short overview and links to complexity references, see the README.
By default, the app loads:
cognitive-metrics-settings.json- from the application base directory (
AppContext.BaseDirectory)
If you use the library directly, you can load another file path with ConfigurationLoader.Load("path-to-file.json").
Use this structure:
{
"cognitive": {
"excludeFilePatterns": [],
"excludePatterns": [],
"scoreThreshold": 0.5,
"showOnlyMethodsExceedingThreshold": true,
"showHalsteadComplexity": false,
"showCyclomaticComplexity": false,
"showCouplingMetrics": false,
"showDetailedCognitiveMetrics": true,
"groupByClass": true,
"countElseAsNesting": false,
"countElseIfAsNesting": false,
"metrics": {
"ifCount": { "threshold": 3, "scale": 1.0, "enabled": true },
"elseCount": { "threshold": 1, "scale": 1.0, "enabled": true },
"loopCount": { "threshold": 2, "scale": 1.0, "enabled": true },
"switchCount": { "threshold": 1, "scale": 1.0, "enabled": true },
"tryCatchCount": { "threshold": 1, "scale": 1.0, "enabled": true },
"returnCount": { "threshold": 2, "scale": 5.0, "enabled": true },
"argumentCount": { "threshold": 4, "scale": 1.0, "enabled": true },
"nestingLevels": { "threshold": 1, "scale": 1.0, "enabled": true }
}
}
}excludeFilePatterns(string[]): configured but currently not applied in source file discovery.excludePatterns(string[]): configured but currently not applied in source file discovery.scoreThreshold(double): threshold used in report filtering and summary counts.showOnlyMethodsExceedingThreshold(bool): iftrue, console report only shows methods abovescoreThreshold.groupByClass(bool): groups output by class in console and HTML reports. HTML output also includes a client-side filter (class name / file path) and default ordering by cognitive score: classes by maximum method score in the class (highest first), methods within each class bytotalScore(highest first).countElseAsNesting(bool): controls whetherelseincreases nesting depth.countElseIfAsNesting(bool): controls whetherelse ifincreases nesting depth.showHalsteadComplexity(bool): whentrue, console and HTML reports include Halstead volume, difficulty, and effort columns (metrics are always computed during analysis).showCyclomaticComplexity(bool): whentrue, console and HTML reports include cyclomatic complexity (computed during analysis).showCouplingMetrics(bool): whentrueandgroupByClassistrue, console and HTML reports show class coupling (incoming, outgoing, stability) in each class header. Coupling is always computed during analysis.showDetailedCognitiveMetrics(bool): present in the shipped JSON for parity with other tools; not bound in this project yet (ignored).metrics(Dictionary<string, MetricConfiguration>): scoring rules per metric key.
Each metric entry contains:
threshold(int): score starts increasing only above this value.scale(double): controls growth rate of the logarithmic weight.enabled(bool): enables/disables score contribution for that metric key.
These align with analyzer fields and can contribute to totalScore when enabled is true:
linesOfCode→linesOfCodeScoreifCount→ifScoreelseCount→elseScoreloopCount→loopScoreswitchCount→switchScoretryCatchCount→tryCatchScorereturnCount→returnScoreargumentCount→argumentScorenestingLevels→nestingScorelocalVariableCount→localVariableScorefieldAccessCount→fieldAccessScorepropertyAccessCount→propertyAccessScore
The shipped cognitive-metrics-settings.json enables core control-flow and size metrics (linesOfCode, ifCount, elseCount, argumentCount, returnCount, nestingLevels).
Additional metrics (loopCount, switchCount, tryCatchCount, localVariableCount, propertyAccessCount, fieldAccessCount) are present but disabled by default so existing totalScore behavior is unchanged until you opt in by setting enabled to true.
All listed metrics are counted during analysis and appear in console/CSV output regardless of the enabled flag; enabled only controls whether the metric contributes to totalScore.
showDetailedCognitiveMetrics remains unbound in CognitiveConfiguration (ignored).
From repo root, run the console app with:
dotnet run --project .\CognitiveCodeAnalysisConsoleApp -- [searchPath] [options][searchPath]optional. Defaults to current directory.
-c|--config <path>: loadConfigurationLoader.Load(path)from that JSON file (samecognitiveschema as the default file).-r|--report-type <type>:ConsoleText(default),Html,Json,Csv,Sarif,GithubActions, orGitlabCodeQuality.-o|--output-file <path>: output path (default:cognitive-analysis-report).-b|--baseline <path>: path to a JSON baseline snapshot (from a prior-r Jsonrun). When set, all reports include deltas versus that baseline. HTML report shows red ▲ / green ▼ suffixes after values.--coverage-cobertura <path>: optional Cobertura coverage file.--show-halstead: forceshowHalsteadComplexityon for this run (overrides config).--show-cyclomatic: forceshowCyclomaticComplexityon for this run (overrides config).--show-coupling: forceshowCouplingMetricson for this run (overrides config).
CLI boolean flags use Spectre’s optional bool? binding: omit the option to keep the JSON value; pass --show-halstead / --show-cyclomatic to enable display for that run. To force false from the shell, rely on the config file or omit these flags.
# Analyze current folder with console output
dotnet run --project .\CognitiveCodeAnalysisConsoleApp --
# Analyze a specific folder
dotnet run --project .\CognitiveCodeAnalysisConsoleApp -- .\src
# Generate HTML report
dotnet run --project .\CognitiveCodeAnalysisConsoleApp -- .\src -r Html -o .\report.html
# Custom config and enable Halstead + cyclomatic columns for this run
dotnet run --project .\CognitiveCodeAnalysisConsoleApp -- .\src -c .\my-config.json --show-halstead --show-cyclomatic
# Cobertura coverage (optional)
dotnet run --project .\CognitiveCodeAnalysisConsoleApp -- .\src --coverage-cobertura .\coverage.cobertura.xml
# Save a JSON baseline snapshot
dotnet run --project .\CognitiveCodeAnalysisConsoleApp -- .\src -r Json -o .\baseline.json
# Compare against baseline (HTML shows colored deltas)
dotnet run --project .\CognitiveCodeAnalysisConsoleApp -- .\src -b .\baseline.json -r Html -o .\report.html