I create an Azure Function from the newest Visual Studio template and deploy it to Azure. I have setup Application Insights in the Azure portal so the actual code doesn’t yet know about AppInsights. So far everything works.
Now I want to log some custom telemetry. What do I need to do?
I’ve tried the following:
- Taking a dependency on
TelemetryClient
- DI cannot resolve it
- Taking a dependency on
TelemetryConfiguration
- DI cannot resolve it
- Taking a dependency on
IOptions<TelemetryConfiguration>
and creatingTelemetryClient
fromoptions.Value
- Nothing happens. I guess just having the
APPINSIGHTS_INSTRUMENTATIONKEY
key in configuration is not enough
- Nothing happens. I guess just having the
What to do?
2
Answers
App Insights, with automatic dependency collection and correlation between application calling each other, can be used by adding the following package to the Azure Function.No other AppInsights related packages are needed to be installed.
and adding having the following program.cs
Then TelemetryClient can be used though DI and all "normal" data will be collected out of the box.
Note that the package is still in preview.
I have reproduced in my environment and got expected results as below:
.csproj:
local.settings.json:
Function.cs:
host.json:
Output:
Then in Portal:
Try to follow the above steps to get telemetry client traces in portal and you will get required results as I have got.
Program.cs: