Testing your Azure Function within Azure

Adam Murchison, 08 July 2020

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.

How to navigate to test/run in Azure

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’

image

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’.

image

From here, there’ll be two different key areas, the Test/Run button and the Logs area.

image

Once you select both these buttons, you’re good to go and it should look like this.

image

Logging in a function

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:

log.Info("This is some information about your function.");
log.Warning("This is a warning that occurred in your function.");
log.Error("This is an error that occurred in your function.");

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:

TraceEvent traceEvent = new TraceEvent(TraceLevel.Info, "An event that occurred in your code, that you now want to trace");
log.Trace(traceEvent);

Lets see how they behave in Azure Application Insights.

Running the function, and showing the logging

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:

[FunctionName("Function1")]
public static void Run([QueueTrigger("queuetest", Connection = "")]string myQueueItem, TraceWriter log)
{
     log.Info($"C# Queue trigger function processed: {myQueueItem}");

     log.Info("This is some information about your function.");
     log.Warning("This is a warning that occurred in your function.");
     log.Error("This is an error that occurred in your function");
}

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”.

image

image

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:

image

I hope this example helps you when testing Azure functions within Azure by allowing you to see live logging.