Click here to Skip to main content
15,891,513 members
Articles / Hosted Services / Azure

Enabling Diagnostics in Azure Cloud Services and Virtual Machines: Part 1

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
22 Sep 2014CPOL3 min read 7.3K  
How to enable diagnostics in Azure Cloud services and virtual machines

Azure diagnostics enables us to capture diagnostics data from a worker role, web role, or virtual machine running in Azure. The captured data is stored in Azure storage and can be used for debugging and performance monitoring.

We can configure Azure diagnostics at two stages:

  • Configure diagnostics before deployment. Programmatically, in our code or by using the role designer in Visual Studio or by directly modifying the configuration files. And our changes will take effect the next time that we deploy the Cloud service to Azure or run the service in the emulator.
  • Configure diagnostics at runtime, by using Server Explorer to change the diagnostics settings for a running cloud service.

Configuring Diagnostics Before Deployment

Using Visual Studio, we can customize the diagnostics data that we collect for a role. The changes we do to the diagnostics settings, get saved in the configuration file diagnostics.wadcfg. The changes are reflected when we re-publish our cloud service.

Configuring using Visual Studio

  1. In solution explorer, select the role and double click on it or right click on it and select the Properties menu item.

    Diagnostics_1

  2. This will open up the Configuration tab.
  3. In the Diagnostics section, ensure that the Enable Diagnostics check box is selected. Options Errors only, All information, and Custom plan represent the level of error logging. Errors only option requires the least amount of storage and logs only error and skips warnings and tracing messages. All information logs every information and is, therefore, the most expensive option.
  4. Select the Custom Plan option and click on the Edit button.

    Diagnostics_2

  5. This opens up the Diagnostics configuration window, showing each source of diagnostics data that we can get.

    Diagnostics_3

  6. On each of the tabs, we have the option to specify Transfer Period and Buffer Size. Transfer Period specifies the time interval in minutes after which the buffered logs will be pushed to the storage account. Buffer Size specifies the reserved space in the file system for each type of data. The total buffered size cannot exceed the overall diagnostics quota, shown in the lower-left corner of the dialog box. The Log level has the following values (in order from least information to most): Critical, Error, Warning, Information, or Verbose.
  7. For the time being, we will keep it simple and will go with the suggested settings. Therefore, click on OK button.
  8. In order to have our custom information logged, we can make use of the System.Diagnostics API. To generate data in these logs from your application code, add a reference to System.Diagnostics.dll, and write data by using the static methods of the Trace class, such as TraceError and TraceInformation. Let's say we have this in our WorkerRole.cs.
    C#
    using System.Net;
    using System.Threading;
    using Microsoft.WindowsAzure.ServiceRuntime;
    
    namespace MyWorkerRole
    {
        public class WorkerRole : RoleEntryPoint
        {
            public override void Run()
            {
                // This is a sample worker implementation. Replace with your logic.
                Trace.TraceInformation("MyWorkerRole entry point called", "Information");
    
                while (true)
                {
                    Thread.Sleep(10000);
                    Trace.TraceInformation("Working", "Information");
                }
            }
    
            public override bool OnStart()
            {
                // Set the maximum number of concurrent connections 
                ServicePointManager.DefaultConnectionLimit = 12;
    
                // For information on handling configuration changes
                // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
    
                return base.OnStart();
            }
        }
    }
  9. Next, let's start running our service. We will find that Visual Studio starts the Azure emulator automatically, which we can access in the notification area. Right click and select the Show Compute Emulator UI and Show Storage Emulator UI. Two windows will open up showing the compute and storage environments for our service.

    Diagnostics_4

    Diagnostics_5

  10. The compute emulator window will show all the information which we are logging in a console frame. We can further set the level of logging we want to do by right click on the worker role and selecting the Logging level.
  11. In order to see the log files that are generated, navigate to the C:\Users\<Machine_User_Name>\AppData\Local\DevelopmentStorage, edit the DevelopmentStorage.201210 file, so that the LoggingEnabled is true and save your changes. Stop the running service in Visual Studio, stop the compute and storage emulator and again start running the service in Visual Studio.
  12. We can find our log files at C:\Users\<Machine_User_Name>\AppData\Local\DevelopmentStorage\Logs location.

In the next part, we will see how we can directly configure the diagnostics.wadcfg configuration file.

The post Enabling Diagnostics in Azure Cloud Services and Virtual Machines: Part 1 appeared first on Its me !

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Mindfire Solutions
India India
Software developer, working at Mindfire Solutions, having hands on experience in both Windows and web application using C#, ASP.NET, ASP.NET MVC.

Comments and Discussions

 
-- There are no messages in this forum --