Power Apps and Application Insights: A Guide to Logging, Reporting, and Alerting
Application Insights has established itself among (Microsoft) developers. It is an extremely versatile service for monitoring, diagnosing, and whatever.
Now Application Insights can also be connected to (Canvas) Power Apps.
What does this mean in practice?
Let’s find out.
Creating an Application Insights service
First, we need the Application Insights service in Azure. It is created like any other Azure service.
New -> Application Insights.
We choose which subscription, resource group and region the service will be created for. And of course, we give it a name.
After this, the service can be created.
After a short wait, the service is ready for use. We need the Instrumentation Key found in the overview section. Let’s copy it to the clipboard.
Connecting Power Apps to Application Insights
Open Power Apps to be monitored. In the properties of the App, there is a new field: Instrumentation Key.
The key retrieved from the Application Insights service is copied into the field. Power Apps are then saved and published.
Done.
What information is now collected in Application Insight?
Application Insights – Standard content
In the Application map, Power Apps are shown as one object. Data sources (or other connections) and their use are not visible in any way.
If you look more closely, the whole investigate section is more or less full of nothing. After all, there is information about performance (page loading times).
But the Usage section is already useful.
From the Users section, we see that the application has been used 18 times (sessions) in the last 48 hours.
The performance of the application has been good during the review period (screens have loaded in less than 0.339 seconds on average).
When investigating an error, it is useful to see what the user has done with the app during the session. This information can be found in the sessions section of Application Insight.
The User Flows section shows how users progress in the application from screen to screen. It mercilessly exposes application screens that are rarely visited.
All this is great, but the keen-eyed noticed that by default, Application Insights (in the event view) only receives information about Power Apps when the application starts, closes, and opens different screens.
It’s not enough for anything.
Do not worry. The best is yet to come.
Own log traces
I practice, we want to monitor things like the following from the application
- Execution of operations
- adding/editing/deleting a record
- sending an email
- sending the order
- starting printing
- etc
- Errors
- the form has failed to save
- the launched Flow returned an error
- etc
Activity logging helps us understand what users do with the application and how much.
Error logging enables active reaction in error situations. The generated log also makes it easier to find out the actual fault.
How to add these own entries?
Next, we save information from Power Apps to Application Insights every time the odometer value
- is added
- is deleted
- is edited
- user has returned without adding the value
A trace of the recording of a new odometer reading is added to the Update button.
Trace(
"Odometer",
TraceSeverity.Information,
{
User: varCurUser.Email,
Screen: App.ActiveScreen.Name,
OdometerValue: inpOdometerValue.Text,
Action: "Add"
}
)
If it is a matter of editing a previously done mileage reading, it is marked as a different trace (update ).
Trace(
"Odometer",
TraceSeverity.Information,
{
User: varCurUser.Email,
Screen: App.ActiveScreen.Name,
OdometerValue: inpOdometerValue.Text,
Action: "Update"
}
)
When user clicks the trash can, a trace is created of the deletion of the meter reading recording.
Trace(
"Odometer",
TraceSeverity.Information,
{
User: varCurUser.Email,
Screen: App.ActiveScreen.Name,
OdometerValue: TthisItem.Mittarilukema,
Action: "Delete"
}
)
Returning from the screen without saving (cancel) is logged in the same way.
What about errors?
After saving, let’s check if there are any errors (Errors(datasource)). If there is, all information about the error is written to Application Insight.
Patch(
'Leasing kilometrilukema',
Defaults('Leasing kilometrilukema'),
{
Name: Today(),
Mittarilukema: Value(inpOdometerValue.Text)
}
);
ClearCollect(colErrors;Errors('Leasing kilometrilukema'));
If(!IsEmpty(colErrors),
Trace(
"SaveError",
TraceSeverity.Error,
{
User: varCurUser.Email,
Screen: App.ActiveScreen.Name,
Action: "Update/Insert",
OdometerValue: inpOdometerValue.Text,
Error: colErrors.Error,
Message: colErrors.Message,
Column: colErrors.Column
}
)
)
How does these custom logs look like in Application Insight?
Looking at a single user session like this.
And when browsing log events like this.
Investigating the log data
With the help of the query editor for Application Insight, you can make your own queries for the log data. Below is a query that returns all the traces we made ourselves.
Creating alerts
It is good to know the basics of the query language (KQL) used with Application Insights, because you can create alerts with it, for example.
Below is an example of an alert that triggers every time the application traces log item with the title “SaveError”.
traces
| where message == "SaveError"
Creating a Power BI report
Application Insight is strongly a tool for developers. What if we wanted to offer reports on the data it collects for others to use as well?
First, let’s build a query that returns all the information the report needs. After this, the query is saved in a format understood by Power BI (M query).
Next, create an empty query in Power BI and copy the downloaded M query into it.
In this way, we have a ready-made data set in Power BI. And we can start building reports on top of that.
Incredibly easy.
What if the network connection is bad?
Application Insight is a cloud service. What happens if Power Apps doesn’t have a network when it logs?
Power Apps buffers the log entries and sends them to Application Insights when the network is available again!
Summary
The cost of the Application Insights service mainly depends on the amount of stored log data. By default, the data is stored for 90 days, but the time can be extended up to 720 days.
With 90 days of storage, we are usually talking about a very moderate monthly cost.
I would connect all Power Apps in production use to Application Insights. Always.