# 设置分析环境Setting Up a Profiling Environment

.NET Framework 4 中的分析发生了重大更改。There have been substantial changes to profiling in the .NET Framework 4.

• COR_ENABLE_PROFILING：仅当此环境变量存在并设置为 1 时，CLR 连接到探查器。COR_ENABLE_PROFILING: The CLR connects to a profiler only if this environment variable exists and is set to 1.

• COR_PROFILER：如果 COR_ENABLE_PROFILING 检查通过，CLR 将连接到具有此 CLSID 或 ProgID 的探查器（已事先存储在注册表中）。COR_PROFILER: If the COR_ENABLE_PROFILING check passes, the CLR connects to the profiler that has this CLSID or ProgID, which must have been stored previously in the registry. COR_PROFILER 环境变量被定义为字符串，如以下两个示例中所示。The COR_PROFILER environment variable is defined as a string, as shown in the following two examples.

set COR_PROFILER={32E2F4DA-1BEA-47ea-88F9-C5DAF691C94A}
set COR_PROFILER="MyProfiler"


## 环境变量范围Environment Variable Scope

• 如果在 ICorDebug：： CreateProcess 调用中设置变量，则这些变量将仅应用于当时正在运行的应用程序。If you set the variables in an ICorDebug::CreateProcess call, they will apply only to the application that you are running at the time. （它们将也应用于由继承环境的应用程序启动的其他应用程序。）(They will also apply to other applications started by that application that inherit the environment.)

• 如果在“命令提示符”窗口中设置变量，则它们将应用于从该窗口中启动的所有应用程序。If you set the variables in a Command Prompt window, they will apply to all applications that are started from that window.

• 如果在用户级别设置变量，它们将应用于用文件资源管理器启动的所有应用程序。If you set the variables at the user level, they will apply to all applications that you start with File Explorer. 在设置变量后打开的“命令提示符”窗口将具有这些环境设置，从该窗口中启动的任何应用程序也将如此。A Command Prompt window that you open after you set the variables will have these environment settings, and so will any application that you start from that window. 若要在用户级别设置环境变量，请右键单击 我的电脑，依次单击 " 属性"、" 高级 " 选项卡、" 环境变量"，然后将变量添加到 " 用户变量 " 列表。To set environment variables at the user level, right-click My Computer, click Properties, click the Advanced tab, click Environment Variables, and add the variables to the User variables list.

• 如果在计算机级别设置变量，它们将应用于在该计算机上启动的所有应用程序。If you set the variables at the computer level, they will apply to all applications that are started on that computer. 在该计算机上打开的“命令提示符”窗口将具有这些环境设置，从该窗口中启动的任何应用程序也将如此。A Command Prompt window that you open on that computer will have these environment settings, and so will any application that you start from that window. 这表示该计算机上的每个托管进程都将通过你的探查器启动。This means that every managed process on that computer will start with your profiler. 若要在计算机级别设置环境变量，请右键单击 我的电脑，依次单击 " 属性"、" 高级 " 选项卡、" 环境变量" 和 " 系统变量 " 列表中的变量，然后重新启动计算机。To set environment variables at the computer level, right-click My Computer, click Properties, click the Advanced tab, click Environment Variables, add the variables to the System variables list, and then restart your computer. 重启后，变量将在系统范围内可用。After restarting, the variables will be available system-wide.

• The profiler class implements the ICorProfilerCallback and ICorProfilerCallback2 interfaces. 在 .NET Framework 2.0 版中，探查器必须实现 ICorProfilerCallback2In the .NET Framework version 2.0, a profiler must implement ICorProfilerCallback2. 否则，将不会加载 ICorProfilerCallback2If it does not, ICorProfilerCallback2 will not be loaded.

• 在给定环境中一次只能由一个事件探查器分析进程。Only one profiler can profile a process at one time in a given environment. 可以在不同环境中注册两个不同的探查器，但每个探查器必须分析单独的进程。You can register two different profilers in different environments, but each must profile separate processes. 必须将探查器作为进程内 COM 服务器 DLL 实现，后者被映射到与正在被分析的进程相同的地址空间。The profiler must be implemented as an in-process COM server DLL, which is mapped into the same address space as the process that is being profiled. 这表示探查器在进程内运行。This means that the profiler runs in-process. .NET Framework 不支持任何其他类型的 COM 服务器。The .NET Framework does not support any other type of COM server. 例如，如果探查器要监视远程计算机上的应用程序，则它必须在每台计算机上实现收集器代理。For example, if a profiler wants to monitor applications from a remote computer, it must implement collector agents on each computer. 这些代理将批处理结果，并将它们传递给中央数据收集计算机。These agents will batch results and communicate them to the central data collection computer.

• 由于探查器是在进程内实例化的 COM 对象，所以每个被分析的应用程序都将具有其自己的探查器副本。Because the profiler is a COM object that is instantiated in-process, each profiled application will have its own copy of the profiler. 因此，单个探查器实例不一定要处理来自多个应用程序的数据。Therefore, a single profiler instance does not have to handle data from multiple applications. 但是，必须向探查器的日志记录代码添加逻辑，以防日志文件从其他被分析的应用程序进行覆盖。However, you will have to add logic to the profiler's logging code to prevent log file overwrites from other profiled applications.

## 初始化探查器Initializing the Profiler

HRESULT Initialize(IUnknown *pICorProfilerInfoUnk)


## 设置事件通知Setting Event Notifications

ICorProfilerInfo* pInfo;
pICorProfilerInfoUnk->QueryInterface(IID_ICorProfilerInfo, (void**)&pInfo);