Azure Function Startup Errors

If your Azure Function encounters a problem on startup, you’ll see this message when you hit the function site:

Function App response when startup has failed

Then, if you look in the Azure portal, under Overview, you’ll see something unhelpful like this:

Function App Overview — startup error

What had happened here in fact was that I had mis-spelled a configuration setting name, so that when the application then tried to int.Parse(Environment.GetEnvironmentVariable("Foo")) , it failed with a null argument exception. Knowing this, I can make this more resilient by using int.TryParse() but the error message above doesn’t give me a lot of help to find where this error came from.

For whatever reason, the most detail I can see of the startup error in the portal is the exception message and the assembly name (not even the type name) and there’s no call stack to help me know where in my code I caused this error in System.Private.CoreLib (incidentally if you do know how to get the full exception details here then please let me know!).

Initially I thought to add a try/catch with logging around FunctionsStartup.Configure() method, but unfortunately while the services are being configured it’s not possible to resolve a logger from the container.

Since we do get notification on the portal of the error, but just not enough information to diagnose the error, I thought I would try rethrowing the exception with the message enriched to include the information I needed. This seems to work pretty well:

Code to rethrow an exception, including the original call stack in the message

The error is fatal, there’s not a lot can be done here, and as described above we can’t log the error. However, by wrapping the original exception in a new one, which contains as its message the original message and the original exception’s call stack, we now get this much more useful error on the Azure portal:

Function App Overview — startup error with call stack

Armed with the call stack as well as the exception message, it’s now straightforward to track down and fix this error.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store