Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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

1: Application Insights setup: create a new or use an existing Application Insights resource, and then get the Instrumentation key as described here.

Note: Application Insights packages and their dependencies are often updated to new versions, so I do this check to help verify that I’m not breaking dependencies in subsequent steps.

3: Using the Function App’s Kudu console ( https://.scm.azurewebsites.net/DebugConsole ), navigate to D:homesitewwwroot{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:homedataFunctionspackagesnuget

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. Test the TrackTrace telemetry capture:
telemetryClient.TrackTrace(“hello function”, new System.Collections.Generic.Dictionary { { “testkey”, “testvalue” } });
8. Test the exception telemetry capture:
try {
int zero = 0;
int i = 1/zero;
}
catch (Exception ex)
{
telemetryClient.TrackException(ex, new System.Collections.Generic.Dictionary { { “exceptionKey”, ex.ToString() } });
}
9. Wait a few minutes, and then check Application Insights to verify that the events are logged

Share the post

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

×

Subscribe to Msdn Blogs | Get The Latest Information, Insights, Announcements, And News From Microsoft Experts And Developers In The Msdn Blogs.

Get updates delivered right to your inbox!

Thank you for your subscription

×