Functions don’t currently support automatic dependency tracking with Application Insights like Web Apps do. I suspect this is on its way but in the meantime Microsoft suggests adding custom telemetry to get the same result. This apparently works with no problem on the Functions 1.0 runtime, but with the 2.0 runtime it’s a different story.
To add custom telemetry you need to add a reference to the Microsoft.ApplicationInsights nuget package.
The nuget reference is added, compiles and the function works correctly without further changes. However after using any ApplicationInsights type everything still compiles successfully but the following exception is thrown at runtime:
System.Private.CoreLib: Exception while executing function: MyFunction1. FunctionApp5: Could not load file or assembly ‘Microsoft.ApplicationInsights, Version=2.6.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35′. Could not find or load a specific file. (Exception from HRESULT: 0x80131621).
The Solution
After much troubleshooting I eventually noticed the Functions cli has a ApplicationInsights dll in its cli folder:
And the version of the dll is 2.5.0:
The current lastest version (as of the time of this blog post) is 2.6.4. So I removed this reference and added a reference to the 2.5.0 nuget package then everything worked correctly.
The Functions 1.0 runtime includes the 2.4.0 version of Microsoft.ApplicationInsights dll yet still works with the latest version of the nuget package. So I suspect there is simply a assembly referencing tweak the Functions team needs to make to the 2.0 runtime. Until then this workaround should help.
If you were experiencing the same error and this solved it for you or you have further question please leave a comment below.
Happy Developing!