How to check if code is running on Azure Web Apps?

Sometimes it is important to know if your website code is running on Azure Web Apps (Websites) or elsewhere.  Finding this information out programmatically is only a little tricky.

Generally, for Azure Cloud Services you can use RoleEnvironment.IsAvailable to check if code is running in Web/Worker Role.  This process is not the same for Azure Web Apps. The RoleEnvironment code will not work for Azure Websites and will always return false.  Azure Websites runs in IIS and Azure Cloud Services Host Agent is not initialized there.

A simple and easy way to check if a C#-based website is running under Azure Web Apps infrastructure is to check for existence of this environment variable using WEBSITE_SITE_NAME

!String.IsNullOrEmpty(Environment.GetEnvironmentVariable(“WEBSITE_SITE_NAME”))

The content for this variable will be the name of your site.

To see more environment variables that are tracked under your site visit the following link: https://{sitename}.scm.azurewebsites.net/Env

Need help with Azure monitoring needs. Request a Demo from Netreo!

Ready to get started? Get in touch or schedule a demo.