web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

D365 - Azure Application Insights - Log telemetry from Plugin

(1) ShareShare
ReportReport
Posted on by 105

Hi Experts,

We have a requirement of logging custom traces in Azure Application Insights from plugin custom code. 

Can you please help with out with the process that should be followed for insights implementation.

I have seen couple of links online which specified below code with instrumentation key. There wasn't any thing related to azure connections.   

TelemetryClient telemetry = new TelemetryClient();

telemetry.InstrumentationKey = "xxxxxx-xxxxxxx-xxxxxxx-xxxxxxxx";

// Set session data:
telemetry.Context.User.Id = Environment.UserName;
telemetry.Context.Session.Id = Guid.NewGuid().ToString();
telemetry.Context.Device.OperatingSystem = Environment.OSVersion.ToString();

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
stopWatch.Stop();

TimeSpan ts = stopWatch.Elapsed;

var properties = new Dictionary<string, string>
{
{ "correlationid", "1236547" },
{ "stage", "1236547"},
{ "object", "1236547"},
};

telemetry.TrackEvent("TrailInsights", properties, null);

 

Please help me with possible solution.

Regards,

Veer

 

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    Hey Veer, posted a blog on this a little bit back. Shouldn't need any specific azure connection to make this happen since it is just posting to application insights. Is the code not working as you would have expected?

    my post->celedonpartners.com/.../understanding-your-dynamics-crm-instance-with-application-insights

  • Veer Balla Profile Picture
    105 on at

    Hello,

    Thanks for the update. I tried implementing from your blog but stuck with below exception.

    15311.Capture.PNG

    My plugins are registered in sandbox mode which is causing the exception as I was tried to post from plugin  into app insights. Please find the below telemetry code I am using in my plugin.

    TelemetryClient telemetry = new TelemetryClient();
    telemetry.InstrumentationKey = "xxxxxx-xxxxxx-xxxxxxx-xxxxxxxx";
    telemetry.TrackTrace("TrailInsightsTrace");
    telemetry.TrackEvent("TrailInsightsEvent", null, null);
    telemetry.Flush();
    tracingService.Trace("Telemetry Captured");

    Please let me know where I am doing wrong.

    Regards,

    Veer Balla

  • Suggested answer
    JohnAnonymous Profile Picture
    5,241 on at

    Hi,

    Did you take this step: First of all add a reference to the application insights dll. If you are using a crm online instance you’ll want to ilmerge this assembly as part of your project 

  • Community Member Profile Picture
    on at

    Think Martijn is on the right track here

  • Veer Balla Profile Picture
    105 on at

    Hello,

    Yes, I added latest application insights dll reference to my project and did an IL merge with my plugin. Updated the merged dl in registration tool.

    To confirm the exception, I tested by removing the telemetry code and the system seems to be working fine but when I add it back I got stuck with the above specified exception.

    Please let me know if I am missing any thing more.

    Regards,

    Veer Balla. 

  • Dilip Kumar - CRM Profile Picture
    67 on at

    Hi,

    You will get security exception if you add the Application insights assembly to the plugin in sandbox mode. The only way you can do it to add a rest service in Azure and trigger the service from your plugin code. Add all your Application Insight logics in the rest service and pass the required parameters from the plugin.

    Deploy the Azure rest service and add the below code to your plugin,

    var uri = new Uri("learningportalservices.azurewebsites.net/.../success");

    var request = WebRequest.Create(uri);
    request.Method = WebRequestMethods.Http.Get;
    request.ContentType = "application/json";

    using (var response = request.GetResponse())
    {
    using (var reader = new StreamReader(response.GetResponseStream()))
    {
    string result = reader.ReadToEnd();
    }
    }

  • Paul Dowman Profile Picture
    on at

    I agree with Dilip. When I looked at doing this in April 2017 it became apparent that AppInsights isn't compatible with the sandbox mode that all CRM online plugins have to operate in. The reason is that the first thing the AppInsights code does when it is instantiated is ask for the host machine environment variables to get the machine name. Access to the host server environment variables is denied by the sandbox in CRM Online.

    As Dilip suggest above the only option for using AppInsights is to pass the whole plugin data across to an Azure function or event and let that handle the AppInsights load for you. It's a step removed, and you will need to remember that when looking at the data, but it should give you something.

    You could also look at Organisation Insights on your 365 online instance - that will give you some metrics, but obviously doesn't give you the complete end to end view that you would get if you could log directly to your own app insights instance along with the rest of your code base.

  • Community Member Profile Picture
    on at

    For anyone interested, there's an issue on the application insights .Net github regarding this specific issue: github.com/.../797

  • Fede Jousset Profile Picture
    30 on at

    Yes, unfortunately this issue has been open since January 2017 and it's been moved from release to release to the point that right now is not part of any specific one ("Future"):

    https://github.com/Microsoft/ApplicationInsights-dotnet/issues/416

    Let's hope this new issue raised will help to prioritize a fix.

  • Sergey Kanzhelev Profile Picture
    5 on at

    Seems to be caused by this issue: https://github.com/Microsoft/ApplicationInsights-dotnet/issues/762 I'd suggest to upvote an issue on GitHub 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans