Logging Azure Startup Task to WAD

Debugging startup tasks is very challenging so it is useful to log what they are doing.  Say that you have a ServiceDefinition.csdef file that looks like this:

 

<?xml version="1.0" encoding="utf-8"?>

<ServiceDefinition name="Foo" xmlns="https://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2012-10.1.8">

  <WebRole name="Bar" vmsize="Medium" enableNativeCodeExecution="true">

    <Startup>

      <Task commandLine="Startup\ConfigureIIS.cmd" executionContext="elevated" taskType="simple" />

    </Startup>

    ...

 

And within your project you created a Startup folder and included your scripts with Build Action None and Copy Always, then within the scripts you can pipe the output into a log directory like this:

 

SET ConfigureIIS_LogDir=%~dp0Log\

MKDIR "%ConfigureIIS_LogDir%"

%windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.processModel.idleTimeout:00:00:00 >> "%ConfigureIIS_LogDir%ConfigureIIS.txt" 2>&1

%windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.recycling.periodicRestart.time:168:00:00 >> "%ConfigureIIS_LogDir%ConfigureIIS.txt" 2>&1

EXIT /b 0

 

BTW, the %~dp0 is a very special replaceable parameter and as used above means “%ROLEROOT%\approot\bin\Startup\”.  Also notice that using a script to launch exe's is nice because it allows redirecting the output to the log file.

 

Then in diagnostics.wad you can transfer the log to Azure Storage for diagnostics:

 

  <Directories bufferQuotaInMB="1024" scheduledTransferPeriod="PT5M">

    <CrashDumps container="wad-crash-dumps" directoryQuotaInMB="128" />

    <FailedRequestLogs container="wad-frq" directoryQuotaInMB="128"/>

    <IISLogs container="wad-iis" directoryQuotaInMB="128" />

    <DataSources>

      <DirectoryConfiguration container="wad-startup" directoryQuotaInMB="10">

        <!-- Absolute specifies an absolute path with optional environment expansion -->

        <Absolute expandEnvironment="true" path="%ROLEROOT%\approot\bin\Startup\Log" />

      </DirectoryConfiguration>

 

You can also remote desktop to the instance and view the log too.

 

Happy coding...