loading...
Microsoft Azure

Getting Logs From Static Web Apps APIs

aaronpowell profile image Aaron Powell Originally published at aaron-powell.com on ・2 min read

I’ve been doing a lot recently with Azure Static Web Apps, mainly because I find it to be a service that really fits me needs. Sometimes though, I write code that has a bug in it, especially in the Functions backend, and that can be difficult to diagnose.

The other day I found myself in such a situation, the API worked locally, but when deployed to Azure it didn’t. sigh And as the service is in preview the debugging story is still not great. sigh again

Now, we know that the API for the Static Web Apps is Azure Functions and Functions integrates with App Insights, so I figured there has to be some way to tap into that.

First up, we need to create an Application Insights resource in Azure:

Create a new resource

Once the resource is created, copy the Instrumentation Key for it:

Instrumentation Key location

Now we can head over to our Static Web App, then navigate to it’s Configuration and click Add :

Add Configuration

We need to add a new configuration value named APPINSIGHTS_INSTRUMENTATIONKEY which has the value of your Instrumentation Key. Once this is saved the Functions are restarted and connected to App Insights. All that’s left is to start generating some errors and check out the exceptions query.

A bonus tip is that if you want to look at anything you write out to the logs of Azure Functions (context.log in JavaScript), they are available in the traces table.

Conclusion

It’s not immediately obvious how to connect up a Static Web Apps backend API to App Insights, but as it’s Functions under the hood it really only takes a APPINSIGHTS_INSTRUMENTATIONKEY being set. And since configuration values are per-environment, you can use a different App Insights for your non-production instance easily.

And a shout-out to Anthony Chu who pointed me in this direction.

Posted on by:

aaronpowell profile

Aaron Powell

@aaronpowell

Hi, my name’s Aaron Powell and I’m a Cloud Developer Advocate (CDA) at Microsoft. My area of specialty is front-end web dev and .NET (especially F#), but I enjoy doing silly things with technology.

Microsoft Azure

Any language. Any platform.

Discussion

markdown guide