diff --git a/Dynamics365.Monitoring.Plugins/ApplicationInsightsTrackCustomEvent.cs b/Dynamics365.Monitoring.Plugins/ApplicationInsightsTrackCustomEvent.cs index f3098af..d0af2b8 100644 --- a/Dynamics365.Monitoring.Plugins/ApplicationInsightsTrackCustomEvent.cs +++ b/Dynamics365.Monitoring.Plugins/ApplicationInsightsTrackCustomEvent.cs @@ -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; @@ -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. @@ -54,6 +57,7 @@ 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 { @@ -61,16 +65,20 @@ public void Execute(IServiceProvider serviceProvider) { 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()); } } diff --git a/Dynamics365.Monitoring.Plugins/PushMessageToApplicationInsights.cs b/Dynamics365.Monitoring.Plugins/PushMessageToApplicationInsights.cs index e2d901f..a15437f 100644 --- a/Dynamics365.Monitoring.Plugins/PushMessageToApplicationInsights.cs +++ b/Dynamics365.Monitoring.Plugins/PushMessageToApplicationInsights.cs @@ -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; @@ -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); + + } + } } } diff --git a/Dynamics365.Monitoring.WebResources/ConsoleApiDemo.js b/Dynamics365.Monitoring.WebResources/ConsoleApiDemo.js new file mode 100644 index 0000000..a0042ea --- /dev/null +++ b/Dynamics365.Monitoring.WebResources/ConsoleApiDemo.js @@ -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(); + } +}; \ No newline at end of file diff --git a/Dynamics365.Monitoring.WebResources/Dynamics365.Monitoring.WebResources.csproj b/Dynamics365.Monitoring.WebResources/Dynamics365.Monitoring.WebResources.csproj index 434aef1..4f85230 100644 --- a/Dynamics365.Monitoring.WebResources/Dynamics365.Monitoring.WebResources.csproj +++ b/Dynamics365.Monitoring.WebResources/Dynamics365.Monitoring.WebResources.csproj @@ -38,6 +38,7 @@ + \ No newline at end of file