I’ve had a few people ask how to use IntelliTrace for monitoring Windows Services. While this is not a supported scenario, we can make it happen.
As I’ve said in other blog posts, it is possible to use IntelliTrace outside of the IDE. The key to having any process using the CLR monitored by IntelliTrace are 3 environment variables:
The first two environment variables, COR_ENABLE_PROFILING and COR_PROFILER are respected by the CLR. When a process calls CLRCreateInstance (for clr versions >=4) or CorBindToRuntime/CorBindToRuntimeEx, the CLR checks the environment for these environment variables. If COR_ENABLE_PROFILING is set to 0x1 or 1, the CLR will then read the value of the environment variable COR_PROFILER for a CLSID value, and attempt to instantiate a COM object for CLR profiling.
The third environment variable specifies the location for IntelliTrace to pick up its’ settings, reffered to as the Collection Plan. You can find a default Collection Plan (with all diagnostics turned off) in %PROGRAMFILES%\Microsoft Visual Studio 10.0\Team Tools\TraceDebugger Tools\en\CollectionPlan.xml (for non EN-US users, look under TraceDebugger Tools\LANG\CollectionPlan.xml.)
The values to set for the process are:
VSLOGGERCPLAN=<Path-to-the-CollectionPlan.xml> (you need to fill this one in 🙂
It is important to keep in mind that the process being launched must have Read permissions for the Collection Plan.
Additionally, one must also fill in a value for the <LogFileDirectory> element in the <StartupInfo> element of the collection plan. If it is not filled in, the IntelliTrace.exe logger will attempt to write the .iTrace file next to the executable being used. In the case of a service, this is not a writeable location (svchost.exe is located in a system directory.)
So how do we make this work for Services? We have to set up the service’s environment so that it may inherit these environment variables, then when the process starts, IntelliTrace will start collecting. When the process shuts down, the .iTrace file will be readable. Now we have to get the environment set up. We can do this via the registry.
Let’s say that our service is called MyAwesomeService. Then you would want to open RegEdit and navigate to the key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\MyAwesomeService. Under this key, it is possible to set the environment by creating (or modifying if it already exists) a “Multi-String Value” or REG_MULTI_SZ key with the name “Environment”.
Create a Multi-String Value
With the name “Environment”
And populate it with your settings
Once you have inserted the aforementoned environment variables into this Environment key, and restart the service using “net stop MyAwesomeService” followed by “net start MyAwesomeService”. At this point, an IntelliTrace.exe process should have launched (assuming managed code has run in the process). Simply “net stop MyAwesomeService” and open the iTrace file to view what happened.
Hopefully this has been a useful and easy-to-follow blog post. If you have any questions or trouble following these steps, let me know so I can help out and also fix my instructions.