I am not sure it is a good idea to be dependent on some internal implementation like e.g. WEBSITE_INSTANCE_ID
advised in the other answers.
I have just used a very simple approach by defining my own environment variable via local.settings.json
:
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"IS_RUNNING_LOCALLY": true
}
}
then I just check like that:
bool isLocal = Environment.GetEnvironmentVariable("IS_RUNNING_LOCALLY") == "true";
It is a workaround, but it's easy and intuitive and should be rather safe.
I tried with AZURE_FUNCTIONS_ENVIRONMENT
, but one cannot set it in the local file as the runtime complains that it already exists, which seems like very stupid as one could have just set environment to "Local" e.g.
P.S. It seems that relying on e.g. default Environment is risky since there are multiple bugs related to it and you are not quite guaranteed for it to be "Production" as the documentation claims. See e.g. here.