Testing your Azure function from a queue, HTTP request or another trigger can be a pain especially when you’re waiting for the Application Insights to show the log as it can take up to 5 minutes. You can test your function directly within Azure with real-time logging.
For this example, it’s assumed you have Application Insights enabled. I’m using a function that’s triggered by a queue. I’ve created this within Visual Studio and have connected it to Azure. I’ll demonstrate the different types of logging and how to submit messages and log real time to your Queue within Azure.
Navigate to your Function App, mine’s called ‘BlogTestApp’, and then select ‘Functions’
Once here, you may see a bunch of different functions within your app. Select the one you want to test, I’ve only got one function configured: ‘Function1’. From here, select ‘Code + Test’.
From here, there’ll be two different key areas, the Test/Run button and the Logs area.
Once you select both these buttons, you’re good to go and it should look like this.
Before showing the instant logging and running your function in Azure, let’s talk about the TraceWriter class that is within an Azure Function to log data to Applications Insights.
When you create an Azure function, the function has a default parameter ‘log’ of type TraceWriter. There are some key methods here that I’m demonstrating below:
On a side note here, you can trace another way using Trace Events but I personally prefer to use logging like the above as I find it more concise and easier to read, if you’d like to trace using trace events this is shown below:
Lets see how they behave in Azure Application Insights.
The code for my function is very simple, it prints the message you input and logs 3 times with different log levels as shown below:
Within Azure, I head back to Input/Output screen and input my message that I want to send to the queue. We now expect the function to print the logging above in the console displayed in the Logs area once we click “Run”.
You can now see your live logs within Azure. As you can see, there are different levels of logging, information, warning and error. This is very standard, but the errors and warnings are clearer in the text within the console and you can filter your logs by selecting the log level as shown below:
I hope this example helps you when testing Azure functions within Azure by allowing you to see live logging.