DiagnosticMonitor - RoleEnvironmentException was unhandled

I came across this simple RoleEnvironmentException error last night and perhaps because I was sleepy it took me a little time to diagnose. I thought I’d share how I resolved it.

As pointed out in this article its a fairly common problem with a frustrating lack of detail in the error. You receive only error.


In actual fact, by all accounts, this is simply a result of not finding the configuration setting as expected. As pointed out by Doug Seelinger you can reproduce by simply make a typo in your string.

Its worth bearing in mind, at this point, thanks to the SDK 1.3 changes, that after the Diagnostics module is added to a role, the Azure Diagnostics service is started automatically using the default configuration. So the main reason you may want to start the Diagnostic Monitor yourself is if you change the configuration (to include scheduling of transfers and specific diagnostics)

Indeed Neil Mackenzie’s approach may be better:

“A more logical way to change the WAD configuration is to use RoleInstanceDiagnosticManager.GetCurrentConfiguration() to retrieve the current configuration and then use RoleInstanceDiagnosticManager.SetCurrentConfiguration() to update the configuration.

In the meantime here is the dummies guide to fixing the RoleException error:

Make sure you have

  1. Created the "DataConnectionString" configuration setting in csdef
  2. Configured it properly in cscfg
  3. Been running the cloud service project, not the web role project

then in your role entry point use the fully qualified connecting string syntax:

            DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", config);

I found that How to Initialize the Windows Azure Diagnostic Monitor contains these steps but if you are still using the  DiagnosticMonitor.Start Method make sure you qualify the fully path name (as highlighted in yellow above). If you don't fully qualify the name (as outlined here) you may well receive the dubious “error”.