共用方式為


Using IntelliTrace with Services

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:

  • COR_ENABLE_PROFILING
  • COR_PROFILER
  • VSLOGGER_CPLAN

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:

COR_ENABLE_PROFILING=1

COR_PROFILER={301EC75B-AD5A-459C-A4C4-911C878FA196}

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.

--Matthew

Comments

  • Anonymous
    October 09, 2013
    Hi I have tried this way for the windwos services, but .itrace file is not created in the given StartupInfo path. could you please let me know how to run IntelliTrace for windows service in detail. Thanks you in advance

  • Anonymous
    October 16, 2013
    The comment has been removed

  • Anonymous
    December 09, 2014
    Hi. Can you tell me what I can do if I want to capture itrace log in a server without Visual Studio installed? The profiler is registered as Visual Studio installed right? So in production if I want to capture trace log is really a problem. Is it a way to make it possible? This is really important to me. My contact is TFeng2@slb.com By the way, I love your blog, it's very useful.

  • Anonymous
    December 22, 2014
    Can someone show me a plan.xml file that has a <LogFileDirectory> set properly? I'm trying to follow the directions here, really appreciate the help, but I can't get the service to start with Intellitrace. The error is 0x80040005 which makes sense because I can see that its trying to create the log file in the directory where the service lives. I know I'm doing something wrong, but I can't find a single plan.xml file that has LogFileDirectory set to anything other than blank. Thank you