How to Enable ASP.NET Trace Statements to Show Up In Windows Azure Compute Emulator

As you may be aware, Windows Azure has a cloud simulation environment that can be run on a desktop or laptop computer to make it easier to develop applications for the Windows Azure cloud. One of the tools is the Compute Emulator which simulates the running of Web Roles and Worker Roles as part of Cloud Services. The Compute Emulator is handy for seeing what’s going on with your Cloud Services, including display of logging trace messages from your application or from Azure. A small anomaly in the developer experience is the use of System.Diagnostics.Trace is configured to output to the Compute Emulator – but only when invoked from Web Role or Worker Role processes; trace statements from ASP.NET code (at least when using full IIS) do not appear. This is because ASP.NET processes lack the DevelopmentFabricTraceListener in the Trace.TraceListeners collection (as described long ago by fellow Windows Azure MVP Andy Cross (@andybareweb)).

The assembly needed in Andy’s instructions is hard to find these days (it lives in the GAC) and is undocumented. And you only want to do this in debug code running in your local Cloud Simulation environment anyway. So explicitly referencing the needed assembly feels a little dirty since you’d never want it to be deployed accidentally to the cloud.

The Solution

I’ve taken these considerations and created a very simple to use method that you can easily call from ASP.NET code — probably from Application_Start in Global.asax.cs — and not worry about it polluting your production code or causing other ills. The code uses reflection to load the needed assembly to avoid the need for an explicit reference, and the dynamic loading is only done under the proper circumstances; loading the assembly would never be attempted in a cloud deployment.

The Code

// Code snippet for use in Windows Azure Cloud Services.
// The EnableDiagnosticTraceLoggingForComputeEmulator method can be called from ASP.NET
// code to enable output from the System.Diagnostics.Trace class to appear in the
// Windows Azure Compute Emulator. The method does nothing when deployed to the cloud,
// when run outside the compute emulator, when run other than in DEBUG, or run repeatedly.
// The code uses Reflection to dynamically load the needed assembly and create the
// specific TraceListener class needed.
// protected void Application_Start()
// {
// // .. other config
// EnableDiagnosticTraceLoggingForComputeEmulator();
// }
// public ActionResult Index()
// {
// Trace.TraceInformation("This message ONLY show up in the Windows Azure Compute Emulator" +
// " if EnableDiagnosticTraceLoggingForComputeEmulator() has been called!");
// return View();
// }
// Bill Wilder | @codingoutloud | Nov 2012
// Original:
using System.Reflection;
using System.Diagnostics;
[Conditional("DEBUG")] // doc on the Conditional attribute:
void EnableDiagnosticTraceLoggingForComputeEmulator()
if (RoleEnvironment.IsAvailable && RoleEnvironment.IsEmulated)
const string className =
if (Trace.Listeners.Cast<TraceListener>().Any(tl => tl.GetType().FullName == className))
Trace.TraceWarning("Skipping attempt to add second instance of {0}.", className);
const string assemblyName =
"Microsoft.ServiceHosting.Tools.DevelopmentFabric.Runtime, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
//var path = Assembly.ReflectionOnlyLoad(assemblyName).Location;
//Assembly assembly = Assembly.LoadFile(path);
var assembly = Assembly.LoadFile(assemblyName);
var computeEmulatorTraceListenerType = assembly.GetType(className);
var computeEmulatorTraceListener = (TraceListener)Activator.CreateInstance(computeEmulatorTraceListenerType);
Trace.TraceInformation("Diagnostic Trace statements will now appear in Compute Emulator: {0} added.", className);
catch (Exception)
// eat any exceptions since this method offers a No-throw Guarantee


Bill is the author of the book Cloud Architecture Patterns, recently published by O’Reilly. Find Bill on twitter @codingoutloud or contact him for Windows Azure consulting.

Cloud Architecture Patterns book

3 thoughts on “How to Enable ASP.NET Trace Statements to Show Up In Windows Azure Compute Emulator

  1. Pingback: Stupid Azure Tricks are coming your way | Coding Out Loud

  2. Pingback: Friday, January 17, 2014 on #WindowsAzure | Alexandre Brisebois

  3. Pingback: Friday, January 17, 2014 on #WindowsAzure | TGS Partners

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.