A day with .Net

My day to day experince in .net

Custom Error Logs -Azure

Posted by vivekcek on April 11, 2012

You can collect data in a custom log file. The custom log file is created in a local storage resource. You create the local storage resource by using the LocalResource Class and adding the local storage resource to the configuration of the Windows Azure diagnostic monitor. Perform the following steps to initialize the collection of custom log data.

1. Open the service definition file (CSDEF) using your favorite text editor and add the LocalStorage element. The following example shows the element added to the definition of a web role:

<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="Diagnostics" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
  <WebRole name="DiagnosticWeb">
      <Site name="Web">
          <Binding name="Endpoint1" endpointName="Endpoint1" />
      <InputEndpoint name="Endpoint1" protocol="http" port="80" />
      <Import moduleName="Diagnostics" />
      <LocalStorage name="LocalResource" cleanOnRoleRecycle="false" sizeInMB="200" />

2. Add the following code in OnStart() of WebRole.cs

 LocalResource loc = RoleEnvironment.GetLocalResource("LocalResource");

            DirectoryConfiguration dirconfig = new DirectoryConfiguration();
            dirconfig.Container = "wad-customlogs-container";
            dirconfig.DirectoryQuotaInMB = loc.MaximumSizeInMegabytes;
            dirconfig.Path = loc.RootPath;

            var diaConfig = DiagnosticMonitor.GetDefaultInitialConfiguration();
            dirconfig.DirectoryQuotaInMB = 200;
            diaConfig.Directories.ScheduledTransferPeriod = TimeSpan.FromMinutes(1.0);
            DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", diaConfig);

            File.Create(loc.RootPath + "vivek.txt");

            return base.OnStart();

The above code will transfer the file vivek.txt to wad-customlogs-container.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s