为带注释的云原生应用生成合规性报告

已完成

合规性部门需要能够查看代码和合规性报告,以确保应用程序符合公司的政策。 .NET 合规性框架提供了一种方法来生成显示应用程序的合规性状态的报告。

什么是合规性报告?

在编译时可以生成合规性报告。 .NET 合规性框架生成一个 JSON 文件,其中包含有关应用程序中使用的数据分类和修订方法的详细信息。

{
    "Name": "DataEntities",
    "Types": [
        {
            "Name": "DataEntities.Order",
            "Members": [
                {
                    "Name": "CustomerAddress",
                    "Type": "string",
                    "File": "C:\\Developer\\mslearn-dotnet-cloudnative\\dotnet-compliance\\eShopLite\\DataEntities\\Order.cs",
                    "Line": "25",
                    "Classifications": [
                        {
                            "Name": "EUIIData"
                        }
                    ]
                },
                {
                    "Name": "CustomerName",
                    "Type": "string",
                    "File": "C:\\Developer\\mslearn-dotnet-cloudnative\\dotnet-compliance\\eShopLite\\DataEntities\\Order.cs",
                    "Line": "21",
                    "Classifications": [
                        {
                            "Name": "EUIIData"
                        }
                    ]
                },
    ...

上面的报告是来自 eShopLite.DataEntities 项目的一个示例。 它显示 Order 类有两个分类为 EUIIData 的属性。

{
    "Name": "Store",
    "Types": [
        {
            "Name": "Store.Services.Log",
            "Logging Methods": [
                {
                    "Name": "LogOrders",
                    "Parameters": [
                        {
                            "Name": "logger",
                            "Type": "Microsoft.Extensions.Logging.ILogger",
                            "File": "C:\\Developer\\mslearn-dotnet-cloudnative\\dotnet-compliance\\eShopLite\\Store\\Services\\ProductService.cs",
                            "Line": "103"
                        },
                        {
                            "Name": "order",
                            "Type": "DataEntities.Order",
                            "File": "C:\\Developer\\mslearn-dotnet-cloudnative\\dotnet-compliance\\eShopLite\\Store\\Services\\ProductService.cs",
                            "Line": "103"
                        }
                    ]
                }
            ]
        }
    ]
}

上面的报告是来自 eShopLite.Store 项目的一个示例。 它显示 ProductService 类中的 LogOrders 方法采用 Order 对象作为日志记录参数。

如何生成合规性报告

对于要生成报告的每个项目,你需要采取两个步骤:

  1. Microsoft.Extensions.AuditReports NuGet 包添加到每个项目。

  2. 将两个条目添加到 csproj 项目文件的 PropertyGroup 节中:

    <GenerateComplianceReport>true</GenerateComplianceReport>
    <ComplianceReportOutputPath>$(MSBuildThisFileDirectory)\path to folder location</ComplianceReportOutputPath>
    

    第一个条目启用合规性报告生成。 第二个条目指定将生成报告的文件夹的路径。 文件名为 ComplianceReport.json

进行这些更新后,在解决方案文件夹中运行 dotnet build 会为 GenerateComplianceReport 属性设置为 true 的每个项目生成一个合规性报告。