Dynamics 365 F&SCM custom services logs and tracking using Azure application Insights

Dynamics 365 F&SCM custom services logs and tracking using Azure application Insights

Recently I have been working in a migration project from AX2012 to D365 F&SCM that has a high number of integrations with external systems that were implemented using AIF.

After re-implement the AX2012 AIF inbound services to custom services in D365 F&SCM I started to look at the requirements related with log & trace of the service requests.

While in AX2012 we had a good logging mechanism implemented as part of the framework where AIF requests & responses were tracked using a set of tables (AIFMessageLog) and available in the product UI,

In D365 F&SCM this is not available any more in the standard.

In the current version this has been replaced with LCS telemetry that can be used to identify what endpoint is hit and how much time it took but It won't have the payload, logging of incoming message, correlation to RecId etc…

In addition to this LCS does not provide mechanisms to expose this logs, alert on them or analysis features.

So what options do we have if we want to have full message log of the service requests for tracking and troubleshooting?

Here are our options:

Option 1 – Utilize Azure API management.

This will encapsulate the Dynamics 365 URL.

Azure API provide logging functionalities.

Azure API Management overview and key concepts | Microsoft Docs

This can be a good option and provides advanced functionality, but adds an extra layer that requires configuration and management effort.

Option 2 - Add table/form in D365 F&SCM

When call is made to custom service, log to the form / table etc. Later utilize Batch job to clean-up the history.

This option is kind of replicate the old logging mechanism provided in AX2012 where we can create a set of custom tables and store request and response info for each service call.

This option will require to perform extra processing & database operations as part of service calls.

Will increase our database size and will require implementation of some clean-up process to clean-up this tables with some periodicity.

And will require F&SCM access to see the logs or expose them in some way to be consumed externally.

Option 3 – Utilize Application insights

Emit custom telemetry from Dynamics 365.

What is Azure Application Insights? - Azure Monitor | Microsoft Docs

Explore .NET trace logs in Application Insights - Azure Monitor | Microsoft Docs

Application insights is a great tool for logging & monitoring and provide analytic tools, alerting, integration with DevOps among other features.

It provides a REST API that can be used to work with the logs programmatically

The impact on your app's performance is small. Tracking calls are non-blocking, and are batched and sent in a separate thread.

Using Application Insights from D365 F&SCM

In my case I selected Application insights from the options above because fits with my requirement for logging custom service requests but as well because it could be used to log other aspects of the product that can give more insights and help to troubleshoot different issues, monitor processes and track events and usage.

Application Insights can be used to log:

So despite my requirement is only related with request, once D365 F&SCM is integrated with Application Insights it could be leveraged in the future for other requirements.

Another reason for me to choose this option is that the other systems that interact with D365 F&SCM use application insights as well as logging mechanism and provide a unique correlation ID in each communication.

So this option will allow us to implement full traceability of the messages across systems in a single place by correlation ID

Implementation

There are different options for implementation, see details in the link below

Explore .NET trace logs in Application Insights - Azure Monitor | Microsoft Docs

In my case I will be using the Trace API directly

Application Insights API for custom events and metrics - Azure Monitor | Microsoft Docs

For this we just need to get the instrumentation key of our application insights.

And reference the Microsoft.ApplicationInsights.dll.

In my case I have created a custom C# dll to implement the methods I need to consume the Microsoft.ApplicationInsights.dll to make it easier to use from X++.

The handler DLL will enable to:

  •  Set context properties like user, operation, version & custom global properties.
  • Instance and track telemetry for Events, Exceptions, Requests, Metrics, Traces
  • Add custom properties & custom metrics related with Events, Exceptions, Requests, Metrics, Traces.

You can get the full code for my DLL class library project here:

kiko-zanon/AppInsightsHandler (github.com)

(this is just an initial version of the DLL for proof of concepts that can be improved in many ways)

to use the DLL from C# we just need to add reference to Microsoft.ApplicationInsights.dll and AppInsightsHandler.dll

No alt text provided for this image

And use it as shown below passing the instrumentation key and the tracking type as string.

The example below sends an event with an additional property and metric related

No alt text provided for this image

Traces sent can take a few minutes to appear in Application Insights.

No alt text provided for this image

The below shows how to send a request telemetry

No alt text provided for this image

Like that we can use the DLL to send the other types of traces.

Implementation in D365 F&SCM

Once we have the DLL we can proceed with the implementation in F&SCM.

First I created a parameters table & form to store the integration key and other parameters.

Added the DLL’s as references in AOT

Then I created a class hierarchy that leverages the DLL implementing for the different telemetry types.

No alt text provided for this image

As I want to use trace for all my services I implemented a Service base class that will implement the tracking and all service classes will extend from it.

in my service base class i will have a method that will get as parameter required details that we want to log as properties (request and response) and other details to identify the service.

No alt text provided for this image

Once we have this we just need to call this method passing request and response to it just before return the response in each of our services.

No alt text provided for this image

Here we have some examples of how the requests look in Application Insights

No alt text provided for this image

As we can see we will have the request body and response body serialized as JSON as we added them as properties.

No alt text provided for this image
No alt text provided for this image

We can as well additionally send ExceptionTelemetry messages if we want to save the exceptions.

in my case if there is some error i loop the infolog where exception type is error and i send a ExceptionTelemetry object with the same correlation ID as the order so we can relate the error request with exceptions.

No alt text provided for this image


No alt text provided for this image


note that there are size limitations depending on the type of tracking we send.

you can check the limitations for each type here:

Azure Application Insights Telemetry Data Model - Azure Monitor | Microsoft Docs

I hope this can be useful for anyone looking for monitoring options for D365 F&SCM.

Enjoy!

Daniel Parodi

Dynamics 365 Finance, Dynamics 365 Supply Chain Management, Dynamics 365 Commerce, Entrepreneur

2 年

Great post!

回复
Paquay Renaud

Dynamics 365 Technical architect

3 年

Awesome !

回复

Thanks

回复
Aitor Martin

Dynamics 365 F&SCM FO / AX senior developer and consultant

4 年

Good work Francisco and thanks to share it.

回复

要查看或添加评论,请登录

Francisco Zanon的更多文章

  • Using Graph mail API to retrieve email attachments from inside D365

    Using Graph mail API to retrieve email attachments from inside D365

    In my current project the business receives sales orders by email as a PDF document attached to the email. Today…

    5 条评论
  • Execute direct SQL query from X++

    Execute direct SQL query from X++

    In this post we will review an approach to run Transact SQL scripts to retrieve data for a report data provider from…

    3 条评论
  • Mass import product images in D365 F&SCM using data entity

    Mass import product images in D365 F&SCM using data entity

    In this post we will show how to mass import of product images using data management framework with data entity Product…

    8 条评论
  • D365 F&SCM leveraging table extension framework

    D365 F&SCM leveraging table extension framework

    Introduction In most of the implementation projects where I have participated, there is always requirements to extend…

    3 条评论
  • Adding OCR capabilities to D365 F&SCM using Azure Cognitive services Computer Vision

    Adding OCR capabilities to D365 F&SCM using Azure Cognitive services Computer Vision

    Introduction In many situations we can get requirements to import and process scanned documents into D365 F&SCM…

    4 条评论
  • Add document attachment in D365 F&SCM from PowerApps

    Add document attachment in D365 F&SCM from PowerApps

    Introduction In some situations we can have requirements to allow attachment of documents related with certain tables…

    6 条评论
  • Process Automation Framework

    Process Automation Framework

    Introduction Process automation enables simple scheduling of processes that will be run by the batch server with an…

    4 条评论
  • D365 F&SCM Data lake integration

    D365 F&SCM Data lake integration

    Introduction In this post we will review the integration between D365F&SCM and Azure data lake. We will see how to…

    9 条评论
  • D365 F&SCM OData with Postman

    D365 F&SCM OData with Postman

    OData endpoints allow expose data entities and can be useful for integration purposes in D365 F&SCM, In this post we…

    5 条评论
  • D365 F&SCM Business Events

    D365 F&SCM Business Events

    Introduction In a recent project i had the possibility to work with business events, in this article i will talk about…

社区洞察

其他会员也浏览了