Azure Functions Logging in Powershell

I wrote an Azure Function in Powershell and while I was developing I found out that my logging was not reporting correctly in Application Insights. When the host logging was set to Trace I would expect that all my debug and verbose would appear in the logs of the Azure Function. This was not the case as only Information, Warning and Error messages are reported.

Only Information, Warning and Error messages are reported by default.

There are a few solutions to have proper debug and verbose logging in Azure Functions and have them respected by the host.json logging settings.

The first is in my opinion the cleanest one as I believe it involves less code to write eventually in all subsequent functions you develop in your project. It involves setting the $DebugPreferences, $VerbosePreferences and $ErrorPreferences in the profile.ps1. Once I set all these preferences to ‘Continue’ I can write my verbose, debug and error (non-terminating) messages without the -verbose, -debug or -ErrorAction switch and all is proper logged by Application Insights depending on the host.json log setting. The downside with this approach is that also module imports and other verbose or debug messages from anywhere are being send to Application Insights. (which I believe is desired anyway in a debugging scenario, but had some disagrees here)

The text in red squares is added and resulted in the white square as output. host.json in “trace” mode

The second method is to add with each statement the proper switch. The advantage here is you have more control on chatty (noise) that is being generated in the debug / verbose logging.

For the debug and verbose logging you have to supply behind each the corresponding switch to have them send to the log stream.

To conclude, personally I think the first method is best as it contains all logging desired once you need it. Also Azure Functions has a great scoping for dedicating function logging as described here. To set for example a trace scope just on one function in your Azure Functions it still reduces a lot of noise. To learn more about PowerShell logging in Azure Functions see the Azure Functions PowerShell Documentation. If you want to work and test yourself with the debug settings, I have uploaded my demo project to my Github Repository where you can review any settings. Happy debugging!