Logging and Analyzing Azure Functions

Post Thumbnail

When you create an Azure Function in Visual Studio you will have a ILogger injected into your method that provides logging services for you. When you run your Azure Functions locally this logger will display messages in the console, but for deployments you can connect Azure Application Insights to your Function to capture these logs, as well as provide a heap of monitoring information. Let’s check out how this works and what you can do with it.

Configure Application Insights Logging

To setup Azure Functions to collect logs through Application Insights is pretty simple, both for production and development. For a production instance you can link Application Insights for your Azure Functions through the Azure portal as per this article here. For development environments create an Application Insights instance and copy the instance Instrumentation key to the values section of your localsettings.json file like this:

"APPINSIGHTS_INSTRUMENTATIONKEY": "<INSTRUMENTATION_KEY>" 

Using the in built reports

The default performance reports for Azure Functions is quite good for real time performance monitoring, even locally. It gives a great view of:

  • the resources allocated to the Functions;
  • how many successful and failed requests have been processed by the Function instance
  • how many exceptions have been thrown

The thing to remember about these performance reports is it gives you a good view of all your Functions within your project.

Structuring your own logs

Before we build a custom report, let’s go into the “Logs (Analytics)” item in the Monitoring section of our Application Insights and have a look at some of the Trace Logs. In my function I have the following log code line:

log.LogInformation($"New Message Event Triggered On: {eventGridEvent.Topic} with the Subject: {eventGridEvent.Subject}"); 

This code line generates a log with the event grid topic and subject that triggered my Function. In the output in the trace log the corresponding event has a property called prop__{OriginalFormat} contains this data:

New Message Event Triggered On: /subscriptions/<SUBID>/resourceGroups/gpgmbot/providers/Microsoft.EventGrid/domains/GameMasterBotEvents/topics/messages with the Subject: NewMessage 

If we change up the way we log Functions to adopt a more structured approach, Application Insights will separate out the properties for us, as per this article. In our example I have changed the log code to adopt this approach:

log.LogInformation("New Message Event Triggered On: {Topic} with the Subject: {Subject}", eventGridEvent.Topic, eventGridEvent.Subject);

The output we get now includes the following properties:


prop__Subject	-	NewMessage
prop__Topic	-	/subscriptions/<SUBID>/resourceGroups/gpgmbot/providers/Microsoft.EventGrid/domains/GameMasterBotEvents/topics/messages
prop__{OriginalFormat}	-	New Message Event Triggered On: {Topic} with the Subject: {Subject}

This allows us to build a lot more information into our logging, especially for larger Functions that perform a number of activities.

Building custom reports

We can also build our own reports using custom queries in the “Logs (Analytics)” area. In the above report I am using the operation_Name property to filter the data. For Azure Functions this property is the name of the Function. If your Functions are small this allows you to really break down individual events and build granular reports.

Summary

As you can see, Application insights can give you some good logging and reporting abilities built straight out of the box. This allows you to track and trace your Functions to a very granular level, build monitoring reports, both in production and through development. You could further add to this by automatically export this data and importing it into tools such as PowerBI for further visualizations.


comments powered by Disqus
About Me

Hi, I'm Glenn!

As the Director of IT Operations, I am responsible for leading a team of IT professionals across both IT support and IT operations. I am an experienced, pragmatic IT professional that has helped organizations with transformational projects. I have a diverse experience across development and infrastructure, I am passionate about learning new technologies and solving complex problems. I also occasionally develop web applications and write.

About Me