Add Application Insights telemetry to your Azure Function (for TrackTrace and TrackException)


You can use Azure Application Insights to help debug application issues in a .NET-based Azure Function App. Below are basic steps on how to do this. In certain cases, the Microsoft.ApplicationInsights package alone might be sufficient for your purposes. For this example, I am using the Microsoft.ApplicationInsights.WindowsServer package because it includes additional functionality. More information about the ApplicationInsights telemetry modules is found here.

 

1. You will need an Application Insights resource and instrumentation key. Information about how to create an Application Insights resource and obtain the Instrumentation key is provided here.

 

2. Determine which Application Insights dependencies are needed. In a new, local Visual Studio Console project, install the Microsoft.ApplicationInsights.WindowsServer NuGet package, and then check the project’s packages.config to see which Microsoft.ApplicationInsights.x dependencies are installed.

Note: Application Insights packages and their dependencies are often updated to new versions, so I do this check to help verify that I have the correct dependency versions for subsequent steps.

 

3. Using the Function App’s Kudu console ( https://<functionappname>.scm.azurewebsites.net/DebugConsole ), navigate to D:\home\site\wwwroot\{functionname} and then edit the project.json . Add the Application Insights dependencies that you determined in the previous step. Here is what my project.json looks like:

{
“frameworks”: {
“net46”:{
“dependencies”: {
“Microsoft.ApplicationInsights”: “2.3.0”,
“Microsoft.ApplicationInsights.Agent.Intercept”: “2.0.7”,
“Microsoft.ApplicationInsights.DependencyCollector”: “2.3.0”,
“Microsoft.ApplicationInsights.PerfCounterCollector”: “2.3.0”,
“Microsoft.ApplicationInsights.WindowsServer”: “2.3.0”,
“Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel”: “2.3.0”,
}
}
}
}

 

4. After you compile the Function (ie, run the Function), check to verify that the dependencies are listed under D:\home\data\Functions\packages\nuget

 

5. Add the following references to the Function code file that you need to capture the AI telemetry on:

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.WindowsServer;

 

6. In your code file,  instantiate a TelemetryClient

var telemetryClient = new Microsoft.ApplicationInsights.TelemetryClient();
telemetryClient.InstrumentationKey = “your app insights key from step 1”;
7. Here is a sample of how to add the TrackTrace telemetry to track the value of a variable in your app:
telemetryClient.TrackTrace(“myfunctiontag”, new System.Collections.Generic.Dictionary<string, string> { { “testkey”, somevalue.ToString() } });
8. Here is a sample of how to track an exception in your app:
try {
int zero = 0;
int i = 1/zero;
}
catch (Exception ex)
{
telemetryClient.TrackException(ex, new System.Collections.Generic.Dictionary<string, string> { { “exceptionKey”, ex.ToString() } });
}
9. Wait a few minutes, and then check Application Insights to verify that the events are logged. Information on how to use Application Insights search & filtering mechanisms to check for logged events is found here.

Comments (0)

Skip to main content