Skip to main content

Notifications

Dynamics 365 Community / Blogs / Jason Lattimer's Blog / Log to Application Insights...

Log to Application Insights from Microsoft Flow or Logic Apps

If you haven’t checked out Azure Application Insights yet you might want to give it a look. It’s got a lot to offer in terms of  logging, monitoring, alerting, multi-colored charts and graphs, etc. Microsoft provides libraries for several languages to make logging things easier but ultimately you’re just making HTTP requests to a public endpoint.

There isn't a connector which lets you write to Application Insights but logging can be done fairly easily with a HTTP Action. I’d suggest referencing the telemetry API to figure out what options are available and how data needs to be structured as my examples won’t contain everything available.

Overview

All the requests will be:

Method: POST
Uri: https://dc.services.visualstudio.com/v2/track

In each request body:
  • Replace “time” with the Flow utcNow() function
  • Replace "00000000-0000-0000-0000-000000000000" with your Application Insights key
  • Replace “properties” with any custom key/value pairs you wish to track

Trace

Diagnostic log messages

Body:
  • Replace “message” with whatever you want to trace

Event

User actions and other events

Body:
  • Replace “name” with the name of the event
  • Replace inside “measurements” with a measurement name (string) and value (numeric) or set to null if not using

Metric

Performance measurements such as queue lengths not related to specific events

Body:
  • Replace inside “metrics” with a metric name (string), kind (integer), value (double) – see API for additional details

Dependency

Logging the duration and frequency of calls to external components that your app depends on

Body:

Recommended replacements, doesn’t need to follow this format exactly
  • Replace “id” with an id of some sort - Application Insights libraries have a way of generating an id if you care to track it down
  • Replace “name” with the HTTP verb and Url Absolute Path
  • Replace “resultCode” with the HTTP Status Code
  • Replace “duration” with the elapsed time
  • Replace “success” with true/false
  • Replace “data” with the HTTP verb and Url
  • Replace “target” with Url Host
  • Replace “type” with something describing the type of request
Tracking the time it took to execute a different HTTP request within a Flow was a little difficult. Catching the request Start and End times was fairly easy but I couldn’t seem to find a way to easily calculate the duration. I cheated a little and created an Azure Function to do it for me – not exactly ideal but whatever. Once I had that I could plug the “Calculate Duration” HTTP Action Body into the “duration”. The “resultCode” and be pulled directly from the HTTP Action. To populate “success” I used a simple formula which looked at the Status Code and set accordingly.

if(equals(outputs('Test_HTTP')['statusCode'], 200),true, false)

image

The Azure Function to calculate duration looks like this:

And that’s about it, you should start seeing results in your Application Insights instance.

https://github.com/jlattimer/MSFlowAppInsights

Comments

*This post is locked for comments