Trace the flow of a Cloud Services application with Azure Diagnostics

Tracing is a way for you to monitor the execution of your application while it is running. You can use the System.Diagnostics.Trace, System.Diagnostics.Debug, and System.Diagnostics.TraceSource classes to record information about errors and application execution in logs, text files, or other devices for later analysis. For more information about tracing, see Tracing and Instrumenting Applications.

Use trace statements and trace switches

Implement tracing in your Cloud Services application by adding the DiagnosticMonitorTraceListener to the application configuration and making calls to System.Diagnostics.Trace or System.Diagnostics.Debug in your application code. Use the configuration file app.config for worker roles and the web.config for web roles. When you create a new hosted service using a Visual Studio template, Azure Diagnostics is automatically added to the project and the DiagnosticMonitorTraceListener is added to the appropriate configuration file for the roles that you add.

For information on placing trace statements, see How to: Add Trace Statements to Application Code.

By placing Trace Switches in your code, you can control whether tracing occurs and how extensive it is. This lets you monitor the status of your application in a production environment. This is especially important in a business application that uses multiple components running on multiple computers. For more information, see How to: Configure Trace Switches.

Configure the trace listener in an Azure application

Trace, Debug and TraceSource, require you set up "listeners" to collect and record the messages that are sent. Listeners collect, store, and route tracing messages. They direct the tracing output to an appropriate target, such as a log, window, or text file. Azure Diagnostics uses the DiagnosticMonitorTraceListener class.

Before you complete the following procedure, you must initialize the Azure diagnostic monitor. To do this, see Enabling Diagnostics in Microsoft Azure.

Note that if you use the templates that are provided by Visual Studio, the configuration of the listener is added automatically for you.

Add a trace listener

  1. Open the web.config or app.config file for your role.
  2. Add the following code to the file. Change the Version attribute to use the version number of the assembly you are referencing. The assembly version does not necessarily change with each Azure SDK release unless there are updates to it.

                <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener,
                    <filter type="" />

    Make sure you have a project reference to the Microsoft.WindowsAzure.Diagnostics assembly. Update the version number in the xml above to match the version of the referenced Microsoft.WindowsAzure.Diagnostics assembly.

  3. Save the config file.

For more information about listeners, see Trace Listeners.

After you complete the steps to add the listener, you can add trace statements to your code.

To add trace statement to your code

  1. Open a source file for your application. For example, the .cs file for the worker role or web role.
  2. Add the following using statement if it has not already been added: using System.Diagnostics;
  3. Add Trace statements where you want to capture information about the state of your application. You can use a variety of methods to format the output of the Trace statement. For more information, see How to: Add Trace Statements to Application Code.
  4. Save the source file.