Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Functions (v2): different logging behavior per environment

I have a Function app with multiple functions (v2, .NET Core). This Functions app is deployed to different environments (= different subscriptions). For development purposes I leverage (ILogger) log quite heavily but I would like to turn the logging off when deploying to other environments than dev. The Function app is deployed via an Azure DevOps pipeline.

Which approach do you recommend to achieve that (in code, via appsettings, via host.json...)?

Thanks

like image 398
quervernetzt Avatar asked Oct 15 '22 12:10

quervernetzt


1 Answers

I haven't found a solution to do that with e.g. variables that can be set via e.g. the app settings and I don't want to refactor the code to use some sort of debug flag (especially as there can be also other settings in host.json that are environment specific).

What I am doing now is to use a PowerShell task in the pipeline to set the content of host.json depending on the environment. I.e. I have a host.json file for each environment in the repository and based on the corresponding environment the main host.json is set to the content of the environment specific host.json.

The PowerShell task looks as following:

###########################
# Get environment variables
$releaseEnvironment = $env:RELEASE_ENVIRONMENTNAME
$currentWorkingDirectory = $env:SYSTEM_DEFAULTWORKINGDIRECTORY

###########################
# Get config data
$configFileName = "$($releaseEnvironment)_host.json"
$configFilePath = "$currentWorkingDirectory$projectPath\Deployment\Host\$configFileName"
$configFileContent = Get-Content -Raw -Path $configFilePath | ConvertFrom-Json
Write-Host "Working with '$configFilePath'..."

###########################
# Update host.json
$pathToHostJsonInUse = "$currentWorkingDirectory$projectPath\host.json"
$configFileContent | ConvertTo-Json -Depth 100 | Set-Content -Path $pathToHostJsonInUse
Write-Host "Updated '$pathToHostJsonInUse'..."

Helpful resources in that context are:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-monitoring#configure-categories-and-log-levels

https://learn.microsoft.com/en-us/azure/azure-functions/functions-host-json

like image 180
quervernetzt Avatar answered Oct 20 '22 17:10

quervernetzt