Skip to content

Adding Application Insights tracing

Ahmad Sattar edited this page Oct 24, 2019 · 3 revisions

Adding Application Insights (AI)

Dynamics 365 facilitates tracing functionality, which offers basic tracing of events and plugin execution. In most applications this type of logging is too simple, and something like Application Insights would be a better fit.

Using this guide, you can add Application Insights tracing to run along the simple tracing Dynamics offers.
This will not prevent sending traces to Dynamics 365

We cover 2 methods of logging to Application Insights:

  1. Traces only
  2. Traces, exceptions and plugin execution as Dependencies

Both methods require an Application Insights resource already created with an instrumentation key.

Examples of plugin files for method 1 and method 2 can be found in separate branches links in their respective wiki entries. Before proceeding, make sure to add Application Insights as a dependency to your Plugins project:

Adding Application Insights to the plugins project

In order to use either of the two methods, you must start by adding the Microsoft.ApplicationInsights NuGet package to your Plugins project. Version 2.11.0 has been used writing this guide, you can simply manage NuGet packages and search for it.

After adding the NuGet package, ILRepack has to be configured to include the newly downloaded package contents. At the bottom of your Plugins.csproj file, make sure your AfterBuild target contains Microsoft.ApplicationInsights.dll and a wildcard for System.Diagnostics.*.dll. These additions should not be at the top of the ItemGroup!

// ..
<ItemGroup>
    <InputAssemblies Include="$(TargetDir)Delegate.XrmFramework.BusinessDomain.dll" />
    <InputAssemblies Include="$(TargetDir)Delegate.XrmFramework.BusinessLogic.dll" />
    <InputAssemblies Include="$(TargetDir)Microsoft.ApplicationInsights.dll" />
    <InputAssemblies Include="$(TargetDir)System.Diagnostics.*.dll" />
    <InputAssemblies Include="$(TargetPath)" />
</ItemGroup>
// .. 

Instrumentation key management

If you wish to retrieve your instrumentation key through a "secret" record, this is possible inside LocalPluginContext constructor. You can use the OrganizationService to fetch a record and construct the DGTracingService with the retrieved key.

var res = this.OrganizationAdminService.Retrieve( /*....*/)./*...*/;
this.TracingService = new DGTracingService(this.TracingService, this.PluginExecutionContext.CorrelationId, res/*...*/);
// this.DGTracingService = (IDGTracingService)this.TracingService;