Profiling a Windows Service
Profiling a Windows Service is like profiling a common language runtime (CLR) application. Both profiling operations are enabled through environment variables. Because a Windows Service is started when the operating system starts, the environment variables discussed in Setting Up a Profiling Environment must already be present and set to the required values before the system starts. In addition, the profiling DLL must already be registered on the system.
After you set the COR_ENABLE_PROFILING and COR_PROFILER environment variables and register the profiler DLL, you should restart the target computer so that the Windows Service can detect those changes.
Note that these changes will enable profiling on a system-wide basis. To prevent every managed application that subsequently runs from being profiled, you should delete the system environment variables after you restart the target computer.
This technique also leads to every CLR process getting profiled. The profiler should add logic to its ICorProfilerCallback::Initialize callback to detect whether the current process is of interest. If it is not, the profiler can fail the callback without performing the initialization.