Dynamics CRM: Setting ILogger With Different Azure Application Insight Subscription

When first time I saw this documentation (about sending Log to Azure Application Insight), I wanted to implement it in my environment. But when I tried to set it from Data Export (Preview) blade, we must have a subscription in the same environment (if you subscribe to Dynamics CRM using xxx@xxx.onmicrosoft.com, then you also need to create Azure Application Insight resource using the same username).

My Dynamics CRM account don’t have Azure Subscription

But before you try this in your organization. You must know that this one is not an official setting and is just for experimentation purposes!

Create Azure Application Insight

Login to portal.azure.com using your id > go to Azure Application Insight > fill in all the information. For example, this is my setting:

Create Azure Application Insight

Once you have done this, you can hit the Review + create button and create the resource.

After the resource is created, you can go to Azure Application Insight > Overview blade > Copy the Instrumentation Key for later use.

Copy The Instrument Key

Update The Organization Setting (CRM)

I create this simple exe to update the setting in my organization:

using System;
using System.Linq;
using System.Threading;
using System.Web.Configuration;
using Entities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling.Connector;

namespace CrmCheck
{
    class Program
    {
        static void Main(string[] args)
        {
            var connectionString = WebConfigurationManager.AppSettings["connectionString"];
            var client = new CrmServiceClient(connectionString);

            var query = new QueryExpression(Organization.EntityLogicalName)
            {
                ColumnSet = new ColumnSet(true)
            };
            var result = client.RetrieveMultiple(query);

            var organization = result.Entities[0];
            var organizationUpdate = new Entity(Organization.EntityLogicalName) { Id = organization.Id };
            organizationUpdate["telemetryinstrumentationkey"] = "8344b9a6-xxx-xxxx-9558-698cd761157d";
            organizationUpdate["orginsightsenabled"] = true;
            client.Update(organizationUpdate);

            Console.WriteLine("Updated!");
            Console.ReadKey();
        }
    }
}

If you wondering what’s the sample ConnectionString, here is my App.Config value:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
	  <add key="connectionString" value="AuthType=OAuth;Username=Temmy.Raharjo@xxx.com;Password=yourpassword;Url=https://xxxx.crm.dynamics.com/;AppId=51f81489-12ee-4a9e-aaae-a2591f45987d;RedirectUri=app://58145B91-0C36-4500-8554-080854F2AC97"/>
  </appSettings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
    </startup>
</configuration>

You can run it and wait for the code to run.

The Plugin Code + Demo

The last step is to create the plugin. Here is the sample of the plugin:

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.PluginTelemetry;
using System;

namespace DemoPlugin
{
    public class Plugin1 : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            var logger = (ILogger)serviceProvider.GetService(typeof(ILogger));
            var pluginExecutionContext = (IPluginExecutionContext)serviceProvider
                .GetService(typeof(IPluginExecutionContext));
            var target = (Entity)pluginExecutionContext.InputParameters["Target"];

            logger.LogInformation("From Plugin: " + target["name"]);
        }
    }
}

The code is so simple. We will get the ILogger object, then because for the demo purpose, I will update the Account.Name, then we only need to get the Target[“name”] to be logged.

Below is the plugin step that I register for the Account table in the message Update:

Create The Plugin Step

Here is the result (Application Log might get a delay so that is why I put it as screenshot only):

The Result

Happy CRM-ing!

2 thoughts on “Dynamics CRM: Setting ILogger With Different Azure Application Insight Subscription

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.