Tracing and Logging on Windows Azure

In a single web page design application based completely on the cloud (SUE-AGILE) we have resolved the need to do a system of tracing and logging suitable for a cloud system.

When you want to perform logging on the cloud, you need to take into account that, although technically it is possible to log on text files, or in the Windows event log, it is difficult to recover this data, because you must log into the remote desktop of the virtual machine running the role you logged with. Since the role is distributed across multiple nodes, the log might in turn be distributed across multiple machines, some of which may not be available (no longer active nodes).

It is therefore necessary to resort to the shared repository, since it could be a database. Windows Azure Table Storage is convenient to exploit, both for performance and for cost.

To use the table storage, you must indicate in the configuration file that you want to configure System.Diagnostic for logging on Windows Azure, using the appropriate listener, DiagnosticMonitorTraceListener, as in the following:

  1. <system.diagnostics><trace autoflush="true"><listeners>  
  2.   
  3. <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics" />  
  4.   
  5. </listeners></trace></system.diagnostics> 

At this point, you need to invoke the method in the Global.asax OnStart() method for booting Windows Azure Diagnostics as in the following:

  1. private void StartDiagnostics()  
  2. {  
  3.     // Get default initial configuration.  
  4.     var config = DiagnosticMonitor.GetDefaultInitialConfiguration();  
  5.     config.Logs.ScheduledTransferLogLevelFilter = LogLevel.Undefined;  
  6.     config.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(5);  
  7.     // Start the diagnostic monitor with the modified configuration.  
  8.     DiagnosticMonitor.Start("DiagnosticsConnectionString", config);  

Then using the very common System.Diagnostic.Trace, the trace will be on the table storage of Azure, in the table WADLogsTable. Just configure the correct value for DiagnosticConnectionString in ServiceConfiguration.cscfg (cloud project).

We can also use the Logging Application Block dell'EnterpriseLibrary that is also built on System.Diagnostic. What we need is to choose a trace listener and configure it to use the same DiagnosticMonitorTraceListener, for example:

  1. <listeners>  
  2.     <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.SystemDiagnosticsTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"  
  3.   
  4. source="ICE.WebRole" type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=2.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"  
  5.   
  6. traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack"  
  7.   
  8. name="Azure Diagnostics Trace Listener" />​  
  9. </listeners>