Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Enable Diagnostic Trace Logging for Windows Azure Compute Emulator from ASP.NET
// 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

This comment has been minimized.

Copy link

jheinzel commented Nov 5, 2015

Thanks for this very useful code snippet. Unfortunately it contains an error. Assembly.LoadFile(assemblyName) should be replaced by Assembly.Load(assemblyName).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.