小编Mad*_*avn的帖子

C#:访问".NET CLR内存类别"的PerformanceCounters

我正在尝试使用PerformanceCounter类通过C#访问位于".NET CLR内存类别"中的性能计数器.但是,无法使用我期望的正确类别/计数器名称来实例化类别

new PerformanceCounter(".NET CLR Memory", "# bytes in all heaps", Process.GetCurrentProcess().ProcessName);
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下代码循环遍历类别和计数器

string[] categories = PerformanceCounterCategory.GetCategories().Select(c => c.CategoryName).OrderBy(s => s).ToArray();
string toInspect = string.Join(",\r\n", categories);

System.Text.StringBuilder interestingToInspect = new System.Text.StringBuilder();
string[] interestingCategories = categories.Where(s => s.StartsWith(".NET") || s.Contains("Memory")).ToArray();
foreach (string interestingCategory in interestingCategories)
{
    PerformanceCounterCategory cat = new PerformanceCounterCategory(interestingCategory);
    foreach (PerformanceCounter counter in cat.GetCounters())
    {
        interestingToInspect.AppendLine(interestingCategory + ":" + counter.CounterName);
    }
}
toInspect = interestingToInspect.ToString();
Run Code Online (Sandbox Code Playgroud)

但找不到任何似乎匹配的东西.是不可能从CLR中观察这些值,或者我做错了什么.

重要的是,环境是在64位Windows 7机器上运行的.NET 4.0.

.net c# memory performancecounter

9
推荐指数
1
解决办法
5679
查看次数

当IEnumerable包含null时使用什么异常类

我已多次看到以下异常情况

    public SomeClass(IEnumerable<T> someValues)
    {
        if (null == someValues)
        {
            throw new ArgumentNullException("someValues");
        }

        int counter = 0;
        foreach (T value in someValues)
        {
            if (null == value)
            {
                string msg = counter + "th value was null";
                // What exception class to use?
                throw new ArgumentException(msg, "someValues");
            }
            counter++;
        }
    }
Run Code Online (Sandbox Code Playgroud)

是否有处理这些案件的指导方针?通常,有任何描述"异常样式"的指南比MSDN文档更详细

.net c# exception

5
推荐指数
2
解决办法
1108
查看次数

在 MSBuild 中解析“静态分析结果交换格式 (SARIF)”

当使用 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 …
Run Code Online (Sandbox Code Playgroud)

c# msbuild static-code-analysis

2
推荐指数
1
解决办法
1240
查看次数