What I wanted to cover was custom properties. These are great for adding more than just a simple message when logging events from your application. If there is specific data that you want to collect telemetry for, then custom properties can be used to capture multiple key/value pairs in a single telemetry event.
When viewing the resulting telemetry event in Application Insights via the Azure Portal you can see the custom properties are now displayed in the event data.
All pretty simple so far right? The next logical thing you will want to do with your custom properties is to query/report on them across many sessions to see what’s happening across all of your users/sessions. For this we step into Azure Monitor Log Queries and we write queries over the same data set that we were navigating via the Application Insight GUI. The language used is Kusto.
Below shows the same event but this time as the result of a Kusto query.
Notice that our custom properties appear as a column called customDimensions. I can’t explain why they are called custom properties in Application Insights and custom dimensions in these queries, it’s inconsistent but that’s just how it works. But our bigger issue is that the custom dimensions are all lumped together in one column. This doesn’t make it easy use the data in queries. Imagine we wanted to select all items where the FirstName custom dimension is “Cat”, because all the key value pairs are lumped together in one column value we can’t directly write this query.
The solution is to first use the extend operator to extract our custom dimensions and promote them to columns in their own right. Once you have these as their own columns they can then be used like other column values. As shown in the query below this allows us to write a query to select just the items where the FirstName custom dimension is “Cat”.
Continue reading the follow up article on on dumping entire complex objects to Application Insights as serialized JSON and then deserializing for reporting 💥
Application Insights acts as middleware for many types of actions. In your example the firstName, lastName properties would be added to all telemetry. What if you want to add those properties only to specific actions, for example: Page Visits but not SQL queries.
While you can apply custom metrics to all types of telemetry collected, the example code in the article actually just applies the custom properties to that single Trace call. No other Trace calls (or any other types of telemetry events) would get those custom properties applied. To have custom properties recorded on all events then the best approach is to use Telemetry Initializers https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-filtering-sampling#add-properties-itelemetryinitializer?WT.mc_id=M365-MVP-5002900
Telemetry Initializers should give you the ability to conditionally add the custom properties to just those calls that you want. I hope this helps, let me know how you get on.