Trace the flow of a Cloud Services (classic) application with Azure Diagnostics
Important
Cloud Services (classic) is now deprecated for all customers as of September 1st, 2024. Any existing running deployments will be stopped and shut down by Microsoft and the data will be permanantly lost starting October 2024. New deployments should use the new Azure Resource Manager based deployment model Azure Cloud Services (extended support).
Tracing is a way for you to monitor the execution of your application while it's 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. Tracing lets you monitor the status of your application in a production environment. Monitoring application status 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 initialize the Azure diagnostic monitor, see Enabling Diagnostics in Azure.
Note
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
Open the web.config or app.config file for your role.
Add the following code to the file. Change the Version attribute to use the version number of the assembly you're referencing. The assembly version doesn't necessarily change with each Azure SDK release unless there are updates to it.
<system.diagnostics> <trace> <listeners> <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=2.8.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics"> <filter type="" /> </add> </listeners> </trace> </system.diagnostics>
Important
Make sure you have a project reference to the Microsoft.WindowsAzure.Diagnostics assembly. Update the version number in the preceding xml to match the version of the referenced Microsoft.WindowsAzure.Diagnostics assembly.
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
- Open a source file for your application. For example, the <RoleName>.cs file for the worker role or web role.
- Add the following using directive if it isn't present:
using System.Diagnostics;
- Add Trace statements where you want to capture information about the state of your application. You can use various methods to format the output of the Trace statement. For more information, see How to: Add Trace Statements to Application Code.
- Save the source file.