.NET 性能诊断追踪采集
dotnet-trace-collect
指导捕获诊断工件以排查生产环境 .NET 性能问题:选择工具、收集性能数据,并理解 Windows 与 Linux、.NET Framework 与现代 .NET、容器与非容器环境之间的权衡。当用户需要帮助选择诊断工具或为 .NET 进程收集性能数据时使用。不适用于崩溃转储(使用 dump-collect)、仓库级性能剖析工作流(使用 profiling)或分析已采集的追踪数据。
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 1
- Forks
- 1
01 前言 Front Matter
- name
- dotnet-trace-collect
- license
- MIT
- description
- Guides capturing diagnostic artifacts to diagnose production .NET performance problems: choosing tools, collecting performance data, and understanding trade-offs across Windows and Linux, .NET Framework and modern .NET, and container and non-container environments. Use when the user needs help choosing a diagnostic tool or collecting performance data for a .NET process. Not for crash dumps (use dump-collect), repository-level profiling workflows (use profiling), or analyzing collected traces.
02 资源文件 8
- 许可证LICENSE1.1 KB
- 其他NOTICE5.6 KB
- 参考文档references/dotnet-monitor.md3.7 KB
- 参考文档references/dotnet-trace-collect-linux.md3.4 KB
- 参考文档references/dotnet-trace-collect.md3.4 KB
- 参考文档references/perfcollect.md6.0 KB
- 参考文档references/perfview.md7.9 KB
- 参考文档references/tool-selection.md16.5 KB
03 SKILL.md 正文
<!-- cratis-ai-managed: skills/dotnet-trace-collect/SKILL.md -->
<!-- Modified by Cratis from dotnet's original (MIT): frontmatter, applicability, navigation and the changes listed in NOTICE. Upstream: https://github.com/dotnet/skills (plugins/dotnet-diag/skills/dotnet-trace-collect/SKILL.md; see NOTICE) -->
.NET Trace Collect
Cratis rules in .cratis/ai/rules/ (including csharp.md, specs.csharp.md, and orleans.md) and the cratis-* skills win where they differ.
This skill helps developers diagnose production performance issues by recommending the right diagnostic tools for their environment, guiding data collection, and suggesting analysis approaches. It does not analyze code for anti-patterns or perform the analysis itself.
When to Use
- A developer needs to investigate a production performance issue (high CPU, memory leak, slow requests, excessive GC, networking errors, etc.)
- Choosing the right diagnostic tool for a specific runtime, OS, or deployment topology
- Setting up and running diagnostic tool commands for data collection
- Understanding trade-offs between available tools (e.g. PerfView vs dotnet-trace)
- Collecting diagnostics from containerized or Kubernetes workloads
When Not to Use
- Reviewing source code for performance anti-patterns (use a code review skill instead)
- Benchmarking during development (e.g. BenchmarkDotNet setup)
- Analyzing collected trace or dump files (this skill recommends tools for analysis, but does not perform it)
Inputs
| Input | Required | Description |
|---|---|---|
| Symptom | Yes | What the developer is observing (high CPU, memory growth, slow requests, hangs, excessive GC, HTTP 5xx errors, networking timeouts, connection failures, assembly loading failures, etc.) |
| Runtime | Yes | .NET Framework or modern .NET (and version, especially whether .NET 10+) |
| OS | Yes | Windows or Linux |
| Deployment | Yes | Non-container, container, or Kubernetes |
| Admin privileges | Recommended | Whether the developer has admin/root access on the target machine |
| Repro characteristics | Recommended | Whether the issue is easy to reproduce or requires a long time to manifest |
Workflow
Step 1: Understand the environment
Determine or ask the developer to clarify:
- Symptom: What they are observing (high CPU, memory leak, slow requests, hangs, excessive GC, HTTP 5xx errors, networking timeouts, connection failures, assembly loading failures, etc.)
- Runtime: .NET Framework or modern .NET? If modern .NET, which version? (Especially whether .NET 10 or later.)
- OS: Windows or Linux?
- Deployment: Running directly on the host, in a container, or in Kubernetes?
- Admin privileges: Do they have admin/root access on the target machine or container?
- Repro characteristics: Does the issue reproduce quickly, or does it take a long time to manifest?
- Workload context: Determine or ask the user if you are running in the context of the workload (i.e., on the same machine or connected to the same environment where the issue is occurring). If so, you can run diagnostic commands directly on their behalf. If not, provide the commands as guidance for the user to run themselves.
Use this information to select the right tool in Step 2.
Read references/tool-selection.md when selecting the relevant diagnostic workflow.
Step 3: Guide data collection
Provide the specific commands for the recommended tool. Load the appropriate reference file from the tool reference lookup table for detailed command-line examples.
Key guidance to include:
Cratis: Downloads or installs a tool: requires the user's approval; prefer a pinned version (
dotnet tool install <tool> --version <x>)
- Installation: How to install the tool if it is not already available (e.g.
dotnet tool install -g dotnet-trace). When recommending multiple tools, provide installation and usage instructions for each one — do not mention a tool without showing how to install and use it. - PID discovery (required before any
-p <PID>command): Verify the target process first (for example:dotnet-trace ps,curl <monitor-endpoint>/processes, orpsinside a container). If the app is expected to be PID 1 in a container, still verify before collecting. - Collection command: The exact command to run, including relevant providers, output format, and duration.
- Container considerations:
- Collecting from inside the container: ensure the tool is installed in the image or use
kubectl cpto copy it in. - Collecting from outside the container: use
dotnet-monitoras a sidecar with a shared diagnostic port (Unix domain socket in/tmp). - Kubernetes:
dotnet-monitoras a sidecar container, orkubectl debugfor ephemeral debug containers.
- Collecting from inside the container: ensure the tool is installed in the image or use
- Long-running repros (Windows/PerfView): show how to use trigger arguments and circular buffer settings.
- Output location: Where the collected file will be saved and how to copy it off the target for analysis.
- Artifact handoff checklist: Include runtime version, OS/kernel, container image tag or build SHA, PID/process name, UTC collection start/end timestamps, exact command used, and final artifact path when handing traces to someone else for analysis.
Step 4: Recommend analysis approach
After data is collected, recommend the appropriate tool for analysis. Do not perform the analysis — just point the developer to the right tool and documentation.
| Collected Data | Analysis Tool | Notes |
|---|---|---|
.nettrace file |
PerfView (Windows), Speedscope (web) | PerfView gives the richest view on Windows |
.etl / .etl.zip file |
PerfView | ETW traces from PerfView or perfcollect |
perf.data.nl from perfcollect |
PerfView (Windows) | Copy the file to a Windows machine and open with PerfView |
Validation
- [ ] The recommended tool is compatible with the developer's runtime, OS, and deployment topology
- [ ] The collection command runs without errors
- [ ] The output file is generated in the expected location
- [ ] The developer knows which analysis tool to use for the collected data
Common Pitfalls
Cratis: Requires the user's explicit approval: downloads and runs a script as root / changes container security. Cratis: Downloads or installs a tool: requires the user's approval; prefer a pinned version (
dotnet tool install <tool> --version <x>)
| Pitfall | Solution |
|---|---|
Using dotnet-trace on .NET Framework |
dotnet-trace only works with modern .NET (.NET Core 3.0+). Use PerfView for .NET Framework. |
| PerfView without admin privileges | PerfView requires admin for ETW tracing. Fall back to dotnet-trace if admin is not available. |
perfcollect in container without SYS_ADMIN |
Containers drop SYS_ADMIN by default. Run with --privileged or add SYS_ADMIN capability, or fall back to dotnet-trace. |
| Huge trace files from long repros | On Windows, use PerfView /StopOn triggers that fire on the symptom you want to capture (e.g., /StopOnPerfCounter, /StopOnGCEvent, /StopOnException) with /CircularMB and /BufferSizeMB. Never trigger on recovery — the circular buffer continuously overwrites old data, so the interesting behavior may be lost by the time collection stops. |
| Diagnostic port not accessible in container | Mount /tmp as a shared volume between the app container and dotnet-monitor sidecar for the diagnostic Unix domain socket. |
| Forgetting to install tools in container image | Add dotnet tool install to your Dockerfile, or use dotnet-monitor as a sidecar to avoid modifying the app image. |
Exposing dotnet-monitor with --no-auth in production |
Keep auth enabled, bind to localhost, and use kubectl port-forward for access. Use --no-auth only for short-lived isolated debugging. |
| Collecting only CPU/thread-time trace for networking issues | CPU and thread-time traces alone do not show HTTP status codes, DNS timing, or connection pool behavior. Add the networking providers (System.Net.Http, System.Net.NameResolution, System.Net.Security, System.Net.Sockets) alongside the thread-time trace. |
| Enabling all networking providers when only one is needed | Each networking provider adds overhead. If the issue is clearly HTTP-level (5xx status codes), System.Net.Http alone may be sufficient. Add DNS, TLS, and socket providers when the root cause is unclear. |
Reference navigation
- Read references/dotnet-monitor.md when working on dotnet monitor.
- Read references/dotnet-trace-collect-linux.md when working on dotnet trace collect linux.
- Read references/dotnet-trace-collect.md when working on dotnet trace collect.
- Read references/perfcollect.md when working on perfcollect.
- Read references/perfview.md when working on perfview.
04 同仓库更多 Skills
.NET SIMD 向量化代码
vectorization
设计、实现、优化和审查 .NET 中的 SIMD 代码:使用 TensorPrimitives、Vector64、Vector128、Vector256、Vector512 或硬件内部函数将标量循环向量化,并审查现有 SIMD 代码(包括泛型 Vector 类型)的契约等价性、尾部处理、内存安全性、可移植性、回退方案和实测性能。当正在编写或审查 .NET 循环或 SIMD 例程时使用。不用于与 SIMD 无关的性能工作。
MSBuild自定义目标编写
target-authoring
编写自定义 MSBuild target 的规范模式:DependsOnTargets 与 BeforeTargets、AfterTargets 的区别,Build-to-CoreBuild 三级模式,以及 XxxDependsOn 链式扩展模式。用于诊断或修复自定义 target 的反模式:Directory.Build.targets 重新定义 SDK target、target 替换而非扩展 CompileDependsOn、Outputs 与 Returns 误用导致返回过期结果、缺少 Inputs/Outputs 导致不必要的重新构建,或缺少 FileWrites 注册。不适用于增量构建调优或一般反模式审查(使用 msbuild 技能),也不适用于非 MSBuild 构建系统。
.NET测试命令执行
run-tests
选择并运行与仓库兼容的 .NET 测试命令:针对 MSTest、xUnit、NUnit 和 TUnit 的 classic、VSTest、bridged MTP 或 native MTP 语法。在运行 .NET 测试或需要给出测试命令/标志时使用:运行全部测试、单个测试、测试类、类别、trait 或目标框架、组合筛选器、--filter-query、--no-build、--diag、TRX、覆盖率收集、崩溃或挂起转储、筛选错误或无法识别的选项。不适用于仅做平台识别(使用 platform-detection)、编写或调试测试代码、解读覆盖率报告(使用 coverage-analysis)、CI 调查、迁移或持续的热重载循环。
.NET 性能剖析
profiling
使用免费的官方 .NET 诊断 CLI 工具在代码仓库中进行性能剖析和运行时调查:CPU、GC 压力、分配增长、异常风暴、锁竞争和慢代码。当 .NET 应用程序需要性能或运行时剖析时使用。不适用于用临时跟踪替代真实的性能或负载测试、无暂停风险计划的生产堆内存采集、崩溃转储(使用 dump-collect),或基准测试设计(使用 microbenchmarking)。
.NET 测试平台识别
platform-detection
识别 .NET 项目的测试平台、框架、命令模式,以及 SDK 样式与经典项目系统,并解析 global.json、项目文件、packages.config、Directory.Build.props 和 Directory.Packages.props 对 MSTest、xUnit、NUnit 和 TUnit 的优先级。当询问“使用哪种测试平台或框架”“VSTest 还是 MTP”或“该项目使用什么测试运行器”时使用,包括桥接设置、UseVSTest 退出选项,以及 VSTest/MTP 配置冲突。不适用于运行或筛选测试、命令、标志、TRX、转储或测试命令错误(使用 run-tests),也不适用于热重载或迁移请求。
Orleans 分布式系统设计
orleans
从每种原语的用途和失效模型出发设计 Microsoft Orleans 系统:grain、数字孪生、状态与数据库、事务、消息传递、流(streams)、计时器(timers)、提醒(reminders)、Durable Jobs(持久作业)、无状态工作进程、grain 服务、启动与托管。当任务需要做出 Orleans 设计决策时使用:检查版本与拓扑、选择原语、实现并验证。不适用于其他 actor 技术栈、批处理、纯关系型 CRUD,或不涉及 Orleans 决策的建议。