How to enable file logging of stderr for Node.js in Azure App Services

By default Node.js will write uncaught exceptions to stderr (“standard error”). This is done as a last resort as it is the easiest/safest way of notifying that something went wrong. As a developer you can write to stderr by calling console.error or process.stderr.write .

Azure App Services can automatically monitor stderr and write whatever is written there to disk. This gives a great backup when other types of logging does not work or when Node.js has ended up in a state where it cannot communication with other logging tools.

If one runs a Google search on how to activate file logging in Azure the answer will be to enable “Application logging” which is automatically disabled after 12 hours. Logging which is only active for 12 hours going forward requires you to know in advance when something will go wrong. Something that you obviously will not know.

There is however a better way to enable stderr file logging that keeps the logs for a retention period or until a disk quota has been reached. The logs are saved on disk in your App Service and can be accessed through Kudo (<yoursite>.scm.azurewebsites.net).

To acces the logs, navigate to Kudo for your App Service, click on Debug Console -> CMD and navigate to the path D:\home\LogFiles\Application . There you will find a number of files named something like xxxxxx-xxxx-stderr-xxxxxxxxxxxxx.txt .

These files contains what has been written to stderr. Note that these files are not saved by default.

Activating these logs consists of two steps:

Activate stderr file logging in Azure (through portal.azure.com or ARM) Enable logging for iisnode (the IIS module responsible for interaction with Node.js)

Active through portal.azure.com

Go to https://portal.azure.com, select your App Service and click on Dignostic logs. Select “File system” for “Web server logging”. Enter 100 as “Quota (MB)” and 99999 as “Rentention period (days)”. Click on Save and wait for Azure to save the setting. Select “Off” for “Web server logging”. Click on Save and wait for Azure to save the setting. Profit! You can now find stderr logs in Kudo.

Leaving “Web server logging” on will alwo work but will consume most of those 100 MBs pretty quickly as information on each HTTP request is saved to disk.

Activate through ARM

If using ARM you can need to set httpLoggingEnabled , logsDirectorySizeLimit and WEBSITE_HTTPLOGGING_RETENTION_DAYS for your App service.

{ "type" : "Microsoft.Web/Sites" , "apiVersion" : "2015-08-01" , "name" : "MyAppService" , "location" : "West Europe" , "dependsOn" : [ "Microsoft.Web/serverFarms/MyAppServicePlan" ] , "properties" : { "name" : "MyAppService" , "serverFarmId" : "MyAppServicePlan" , "siteConfig" : { "httpLoggingEnabled" : false , "logsDirectorySizeLimit" : 100 , "appSettings" : [ { "name" : "WEBSITE_HTTPLOGGING_RETENTION_DAYS" , "value" : "99999" } ] } } . . . rest of ARM template is omitted . }

Activate logging for iisnode

Logging in iisnode can be enabled through iisnode.yml or web.config.

In iisnode.yml simply add loggingEnabled: true .

In web.config add a iisnode section as below:

< configuration > < system.webServer > < handlers > < add name = " iisnode " path = " index.js " verb = " * " modules = " iisnode " /> </ handlers > < iisnode loggingEnabled = " true " /> </ system.webServer > </ configuration >

Once these settings have been set you might need to restart the App Service.