In my previous blog post, we looked at how to authenticate and call the Business Central Admin Center Telemetry API, we ended up with a response message that we could not deserialize into a C# class.
Luckily some guys from Microsoft stumbled upon my blog post and asked for feedback, which resulted in a backlog item on their side to provide key-value pairs in the response.
Today we’ll look at how to transform the response into a class that we can then use to create a JSON message that’s importable by Azure Log Analytics.
Azure Log Analytics is the place where logs from all kinds of different resources come together for further analysis, these resources can be virtual machines, logs from Azure Functions or security events from the Azure platform itself, but there’s also a data collector API and that’s where we will send the Business Central telemetry to.
Transforming the response into a class
To refresh your minds a little, this is what the response looks like:
We cannot deserialize the response to a strong-typed class, so all we can do is deserialize it to a class that holds a list with a list of objects, where the first list is a list of telemetry records and the second list is a list of telemetry properties.
We can now loop over the telemetry records and for every record, loop over its properties.
What we do know about the response, is the order of the properties and the definition of those properties, most importantly the name and the data type.
Based on this information we can create a telemetry class where we will store the values as we loop over the array of property values:
We will now loop over the list of telemetry records and for every record, we will loop over its property values.
Because every property is just an object we need to convert it to the desired type.
P.S. I’m not an experienced C# developer, so please forgive me if there are better ways to do this!
We now have a list of a strongly-typed telemetry records!
Creating & configuring Azure Log Analytics
In order to send data to Azure Log Analytics we first need to create a new Log Analytics workspace within a resource group:
Once the resource is provisioned we’ll need to find the Log Analytics Application Id and Application Secret.
The Application Id is the Workspace Id of the Log Analytics resource and the Application Secret is either the Primary or the Secondary Key, both can be found on the ‘Advanced settings’ tab of the resource:
Configuring the console application
I’ve created three new settings in the appsettings.json:
– LogAnalyticsApplicationId (workspace id)
– LogAnalyticsSecret (primary or secondary key)
– LogAnalyticsLogName (the desired log name, doesn’t have to exist)
Sending the telemetry to Azure Log Analytics
In order to get our telemetry in Log Analytics we’ll use the so-called ‘Data Collector API’, it’s an API that accepts custom data.
That means we can simply serialize our list with telemetry records to JSON and send it directly to Log Analytics.
The API is a little complicated but the sample code in the documentation actually works! You can read all about the API here.
The most complicated part is to build up the authorization header, in this case, the authorization header is based on a string that contains the HTTP method, the byte length of our JSON message, the content-type and a date, which is then hashed with the HMACSHA256 algorithm and our Azure Log Analytics secret key:
Now that we’ve got the signature for the authorization header in place, we can actually post the data to the API:
Note that the ‘time-generated-field’ header is set to ‘Timestamp’, this means it will use the Timestamp field in our JSON message for the TimeGenerated property in Azure Log Analytics.
If you omit this parameter, the TimeGenerated defaults to the DateTime of ingestion and you will end-up with two timestamp fields, one from the telemetry record itself and one from log analytics.
Let’s have a look at the result
We now have a console application that is capable of calling the Business Central Admin Center Telemetry API, transform the response into a strongly-typed class, serialize that class to a JSON message and feed that JSON message to Azure Log Analytics.
The console application now looks like this:
After successfully posting the telemetry data to log analytics it takes a couple of minutes for log analytics to process the data, once it’s processed you’ll be able to query the log like this:
In my next blog post, we’ll have a look at how to analyze the data in Azure Log Analytics by using the Kusto query language (KQL).
P.S. I will upload the source code of the console application to my GitHub and provide it in the next blog post!