Error Handling and Log Management in Canvas Apps - Part 2
Write custom traces directly to Azure Application Insights
In my previous post, I demonstrated how we can connect Canvas app to Application Insights to get useful general information like Browser information, location and device information.
In this post, we will cover how to send our own custom errors and logs to Application Insights.
Trace function allows you to collect:
Granular usage information for controls on the screens.
Which specific users are accessing your app.
What errors occur.
Now, let us go through the steps required to get this done:
Create the Canvas App
Create a blank canvas app
Add 3 buttons to the screen- set the button text to: Log Information, Log Warning and Log Error.
OnSelect of the Log Information button, insert the following snippet:
Trace( "New Information", TraceSeverity.Information, { UserName: User().FullName, UserEmail: User().Email, Remark: "Application started successfully" } ); Notify("Information Logged successfully!");
OnSelect of the Log Warning button, insert the following snippet:
Trace( "New Warning", TraceSeverity.
Warning, { UserName: User().FullName, UserEmail: User().Email, Remark: "No active session" } ); Notify("Warning Logged successfully!");
OnSelect of the Log Error button, insert the following snippet:
Trace( "New Error", TraceSeverity.
Error, { UserName: User().FullName, UserEmail: User().Email, Remark: "No active session" } ); Notify("Error Logged successfully!");
Save and Publish the app.
View Logs in Azure Application Insights
Play the app, not from the studio but from the list of apps.
Click on the Log Information button.
The “Information Logged successfully!” message is displayed.
Log in to the Azure Portal and navigate to the Application Insights resource
Select Logs under Monitoring from the left navigation pane:
Type the following query to filter:
traces | where message == "New Information" | order by timestamp
This will show you the logged data as seen below:
If you expand the CustomDimensions, you will find the custom properties that we added to our trace: Remark, UserEmail, UserName.
Repeat same for Log Warning and Log Error.
That’s it for today.
Next we look at how to handle unexpected errors.