How to transfer IIS logs to storage account in a custom format

The sample code in this blog will help you transfer IIS logs in a custom format to your storage account using Windows Azure diagnostics,

You can implement this by using ServerManager class defined in Microsoft.Web.Administration DLL.

 1. Add Microsoft.Web.Administration DLL to the project reference. (Path of Microsoft.Web.Administration DLL: C:\Windows\System32\inetsrv).

    

2. Set Copy local property of the above DLL to true. (Right click on the DLL -> properties -> copy local = true)

    

3. Copy and paste the below code snippet.

4. Run the WebRole in elevated execution context. (Add the below tag in servicedefinition.csdef file to run the code in elevated privileges).

   <Runtime executionContext="elevated"/>

   

// The below code snippet must be added in WebRole.cs file.

using Microsoft.WindowsAzure;

using Microsoft.WindowsAzure.Diagnostics;

using Microsoft.WindowsAzure.ServiceRuntime;

using Microsoft.WindowsAzure.Diagnostics.Management;

using Microsoft.Web.Administration;

namespace WebRole1

{

   publicclassWebRole : RoleEntryPoint

   {

       publicoverridebool OnStart()

       {

           //============================= Configure diagnostic logging ================================//

      // Obtain a reference to the initial default configuration.

      string wadConnectionString = "Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString";

          CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(wadConnectionString));

           RoleInstanceDiagnosticManager roleInstanceDiagnosticManager = storageAccount.CreateRoleInstanceDiagnosticManager

                                (RoleEnvironment.DeploymentId,RoleEnvironment.CurrentRoleInstance.Role.Name, RoleEnvironment.CurrentRoleInstance.Id);

           DiagnosticMonitorConfiguration config = DiagnosticMonitor.GetDefaultInitialConfiguration();

           config.ConfigurationChangePollInterval = TimeSpan.FromSeconds(30.0);

           //transfer the IIS and IIS Failed Request Logs

           config.Directories.ScheduledTransferPeriod = TimeSpan.FromMinutes(1.0);

           //set the configuration for use

           roleInstanceDiagnosticManager.SetCurrentConfiguration(config);

      //===========================================================================================//

      //============================== Custom diagnostic logging =================================//

      using (ServerManager serverManager = newServerManager())

          {

               string str = RoleEnvironment.Roles.Values.ToString();

               // Note that "_Web" is the name of the site in the ServiceDefinition.csdef,

         // so make sure you change this code if you change the site name in the .csdef    

         Site site2 = serverManager.Sites[RoleEnvironment.CurrentRoleInstance.Id + "_Web"];

               site2.LogFile.LogExtFileFlags = LogExtFileFlags.Date | LogExtFileFlags.Time | LogExtFileFlags.ClientIP | LogExtFileFlags.Host | LogExtFileFlags.BytesSent | LogExtFileFlags.BytesRecv;

               site2.LogFile.Period = LoggingRolloverPeriod.Hourly;

               serverManager.CommitChanges();

          }

      //===========================================================================================//

      returnbase.OnStart();

        }

    }

}

 

You will see the below settings in IIS on the Azure VM.

You can check the IIS logs (in custom format) in the below path on the VM :

C:\Resources\Directory\<DeploymentID>.WebRole1.DiagnosticStore\LogFiles\Web\W3SVC1273337584

 If you are running the application on your local machine, make sure the application is deployed on IIS web server instead of IIS Express (applicable for SDK 1.7).

You can find this setting, when you right click on your cloud project -> properties.

Hope this helps :)