Mad*_*avn 2 c# msbuild static-code-analysis
当使用 MSBuild 针对项目运行各种分析器时,所有失败都将以“静态分析结果交换格式 (SARIF)”格式输出(参见例如https://github.com/sarif-standard/sarif-spec)。例如,构建可能会产生以下结果
{
"version": "0.1",
"toolInfo": {
"toolName": "Microsoft (R) Visual C# Compiler",
"productVersion": "1.1.0",
"fileVersion": "1.1.0"
},
"issues": [
{
"ruleId": "SA1401",
"locations": [
{
"analysisTarget": [
{
"uri": "C:\\SomeFile.cs",
"region": {
"startLine": 708,
"startColumn": 30,
"endLine": 708,
"endColumn": 36
}
}
]
}
],
"shortMessage": "Field must be private",
"fullMessage": "A field within a C# class has an access modifier other than private.",
"properties": {
"severity": "Warning",
"warningLevel": "1",
"defaultSeverity": "Warning",
"title": "Fields must be private",
"category": "StyleCop.CSharp.MaintainabilityRules",
"helpLink": "https:\/\/github.com\/DotNetAnalyzers\/StyleCopAnalyzers\/blob\/master\/documentation\/SA1401.md",
"isEnabledByDefault": "True",
"isSuppressedInSource": "True"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
现在我希望能够以最简单的方式解析上面的数据(如果遇到任何未抑制的问题,则中断构建)。怎么做呢?
附注。最好我也想避免实现我自己的 MSBuild 任务和安装特定的软件(例如 PowerShell 3.0 - ConvertFrom-Json)。
小智 5
有一个 SARIF SDK 可用于处理 SARIF 文件。它以 NuGet 包 Sarif.Sdk 的形式提供,源代码位于 GitHub 上的 Microsoft/sarif-sdk 项目中。有一个 How-To 文档 docs/how-to.md 显示了如何从磁盘读取 SARIF 文件并将其反序列化为一个SarifLog对象;然后您可以浏览 SARIF 对象模型以检查单个结果。
在您的情况下,您对isSuppressedInSource结果的“财产包”中的财产感兴趣。How-To 文档解释了如何检索该属性:
Result result = …;
string isSuppressedInSource = result.GetProperty("isSuppressedInSource");
Run Code Online (Sandbox Code Playgroud)
SARIF 规范可在线获取,并且有一个SARIF 主页,其中包含指向更多信息的链接。
最后:请注意,在 Visual Studio 2015 Update 2 和 Update 3 之间,SARIF 格式发生了显着变化。该格式现在处于稳定的 1.0.0 版本。
(注意:抱歉没有提供 SDK、NuGet 包和 How-To 的直接链接。我没有足够的声望点来发布两个以上的链接。)