Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
Expand Down Expand Up @@ -39,6 +40,8 @@ private string GetValueNode(XmlDocument doc, string key) {
}
#endregion
public void Execute(IServiceProvider serviceProvider) {
Stopwatch timer = new Stopwatch();
timer.Start();
//https://msdn.microsoft.com/en-us/library/gg509027.aspx
//When you use the Update method or UpdateRequest message, do not set the OwnerId attribute on a record unless the owner has actually changed.
//When you set this attribute, the changes often cascade to related entities, which increases the time that is required for the update operation.
Expand All @@ -54,23 +57,28 @@ public void Execute(IServiceProvider serviceProvider) {
tracingService.Trace("Parse and Search Unsecure Config at " + DateTime.Now.ToString());
doc.LoadXml(_unsecureString);
_instrumentationKey = GetValueNode(doc, "instrumentationKey");
PushMessageToApplicationInsights messenger = new PushMessageToApplicationInsights();
PostBody postBody = new PostBody();
postBody.iKey = _instrumentationKey;
try {
tracingService.Trace("Create Custom Event Data DTO at " + DateTime.Now.ToString());
postBody.data.baseData = Events.CreateCustomEventData(postBody.data.baseData, context);
postBody.data.baseData.properties.Add("ExecutionTime", executionTime.ToString());
tracingService.Trace("Send Custom Event Request at " + DateTime.Now.ToString());
PushMessageToApplicationInsights messenger = new PushMessageToApplicationInsights();
messenger.SendRequest(postBody, tracingService);
}
catch (InvalidPluginExecutionException ex) {
postBody.data.baseData = Exceptions.CreateExceptionEventData(postBody.data.baseData, ex, context);
PushMessageToApplicationInsights messenger = new PushMessageToApplicationInsights();
messenger.SendRequest(postBody, tracingService);
throw new InvalidPluginExecutionException(ex.Message);
}

//timer.Stop();
tracingService.Trace("Set expected duration " + timer.ElapsedMilliseconds.ToString());
postBody.data.baseData.properties.Add("ExecutionDuration", timer.ElapsedMilliseconds.ToString());
postBody.data.baseData.properties.Remove("ExecutionDuration");
messenger.SendRequest(postBody, tracingService);
messenger.SendRequestAsync(postBody, tracingService, timer);
tracingService.Trace("Leaving plugin execute " + timer.ElapsedMilliseconds.ToString());
}

}
Expand Down
36 changes: 36 additions & 0 deletions Dynamics365.Monitoring.Plugins/PushMessageToApplicationInsights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
Expand Down Expand Up @@ -36,5 +37,40 @@ public void SendRequest(PostBody message, ITracingService tracingService) {

}
}

public void SendRequestAsync(PostBody message, ITracingService tracingService, Stopwatch timer)
{
using (var client = new HttpClient())
{
Uri requestUri = new Uri("https://dc.services.visualstudio.com/v2/track");

MemoryStream stream1 = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(PostBody), new DataContractJsonSerializerSettings()
{
UseSimpleDictionaryFormat = true
});
message.data.baseData.properties.Add("ExecutionDuration", timer.ElapsedMilliseconds.ToString());
ser.WriteObject(stream1, message);
stream1.Position = 0;
StreamReader sr = new StreamReader(stream1);
HttpResponseMessage response = client.PostAsync(requestUri, new StringContent(sr.ReadToEnd(), Encoding.UTF8, "application/json")).Result;
var json1 = @"{""name"":""Microsoft.ApplicationInsights.Dev.7b18b7f7-3daf-4951-abba-8372cf9b21a9.Event"",""time"":""" + DateTime.Now.ToString() + @""",""data"":{""baseType"":""EventData"",""baseData"":{""name"":""sample"",""measurements"":{""measure1"":111,""measure2"":222},""properties"":{""Actual IP"":""192.168.1.1"",""DeveloperMode"":""true"", ""ExecutionDuration"":""" + timer.ElapsedMilliseconds.ToString() + @"""},""ver"":""2""}},""iKey"":""7b18b7f7-3daf-4951-abba-8372cf9b21a9""}";
HttpResponseMessage responseFaster = client.PostAsync(requestUri, new StringContent(json1, Encoding.UTF8, "application/json")).Result;


//string result = response.Content.ReadAsStringAsync().Result;

//if (response.IsSuccessStatusCode)
//{
// tracingService.Trace("Request success: ");
//}
//else
//{
// tracingService.Trace("Request fail, please check the detailed error: ");
//}
//tracingService.Trace(result);

}
}
}
}
49 changes: 49 additions & 0 deletions Dynamics365.Monitoring.WebResources/ConsoleApiDemo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var ConsoleApiDemo = {
//https://developer.mozilla.org/en-US/docs/Web/API/Console
//
Asserting: function (context) {

},
//
Clearing: function (context) {

},
//
Counting: function (context) {

},
//
Debugging: function (context) {

},
//
Deserializing: function (context) {

},
//
Grouping: function (context) {

},
//
Logging: function (context) {
var someObject = { str: "Some text", id: 5 };
console.log(someObject);

var car = "Dodge Charger";
console.info("My first car was a", car, ". The object is:", someObject);
},
Profiling: function (context) {

},
//
Timing: function (context) {
console.time("answer time");
alert("Click to continue");
console.timeLog("answer time");
alert("Do a bunch of other stuff...");
console.timeEnd("answer time");
},
Tracing: function (context) {
console.trace();
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<ItemGroup>
<Content Include="ApplicationInsightsDemo-UCI.js" />
<Content Include="ApplicationInsightsDemo.js" />
<Content Include="ConsoleApiDemo.js" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>