CodeProjectAzure 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
- In solution explorer, select the role and double click on it or right click on it and select the Properties menu item.
- This will open up the Configuration tab.
- 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.
- Select the Custom Plan option and click on the Edit button.
- This opens up the Diagnostics configuration window, showing each source of diagnostics data that we can get.
- 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
. - For the time being, we will keep it simple and will go with the suggested settings. Therefore, click on OK button.
- 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.
using System.Net;
using System.Threading;
using Microsoft.WindowsAzure.ServiceRuntime;
namespace MyWorkerRole
{
public class WorkerRole : RoleEntryPoint
{
public override void Run()
{
Trace.TraceInformation("MyWorkerRole entry point called", "Information");
while (true)
{
Thread.Sleep(10000);
Trace.TraceInformation("Working", "Information");
}
}
public override bool OnStart()
{
ServicePointManager.DefaultConnectionLimit = 12;
return base.OnStart();
}
}
}
- 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.
- 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.
- 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. - 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 !