Adventures with App Insights: Tracking Custom Events in .NET Core Function Application

WARNING: According to this article, https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#logging-services, is NOT A RECOMMENDED PRACTICE.

While what I have done works, according to the article, adding AddApplicationInsightsTelemetry() to the services collection “registers services that conflict with services provided by the environment.”

In a previous article, we looked at how to track custom events in Azure Application Insights from a .NET Core Web Application, in this article, we will look at how to track custom events from Azure Functions.

Azure Functions support a variety of languages, in this article we will stick with C#.

In Azure, you will need to create a Resource Group, and then create an Application Insights resource.

Create an Azure Function project in either Visual Studio or VS Code..

I named my Azure Function project, FunctionApp1, when prompted for a Trigger type, select HttpTrigger and name the class ValuesHttpTrigger.cs.

Install the NuGet Package Microsoft.ApplicationInsights.AspNetCore to add Application Insights support.

We will also need to install the NuGet Package Microsoft.Azure.Functions.Extensions to add support for dependency injection in our Azure Function project.

In the Azure Portal, navigate to your Application Insights resource and copy the Instrumentation Key.

Open the local.settings.json file.

Replace the ApplicationInsights:InstrumentationKey value with the Instrumentation Key value for your Application Insights resource.

Add a new file called Startup.cs, and past in the contents of the code snippet as follows:

This will wire up Application Insights.

Now on to tracking some events in our Azure Functions!

Update the contents of the ValuesHttpTrigger.cs class with the contents of the code snippet that follows:

Some background on what is going on.

The TelemetryClient is being injected, thanks to our Startup.cs configuration.

In each method we want to track, in Application Insights, the request type and any variables that were passed.

Run the Function App.

In Postman, create requests for the GETPOSTPUT and DELETE against the API methods in our Web Application.

In the Azure Portal, navigate to the Application Insights resource, and click Log Analytics.

You should see a list of your custom events.

Please note, it may take a couple of minutes for the data to actually show up in Azure Application Insights.

Thanks for reading!


Discover more from Matt Ruma

Subscribe to get the latest posts to your email.

1 Reply to “Adventures with App Insights: Tracking Custom Events in .NET Core Function Application”

  1. I’m pretty sure what you have works simply because you’re using the same IConfiguration key as the built in AppInsights code. However, if you removed it from your config json and hard coded an instrumentation key into your code, it would no longer work.

    Really, the code above isn’t actually doing anything. You could remove it all and just leave the instrumentation key in your config json and you’d get the same behavior.

Leave a Reply

Your email address will not be published. Required fields are marked *