Skip to content

Instantly share code, notes, and snippets.

@carfup
Last active May 15, 2018 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carfup/e8350099d8273f666897066feaa1d331 to your computer and use it in GitHub Desktop.
Save carfup/e8350099d8273f666897066feaa1d331 to your computer and use it in GitHub Desktop.
Sample of Azure functions code in order to send CRM Plugins exceptions to Azure Application Insights
/*
* Created by : Clement Olivier
* Date : 05/14/2018
*/
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Newtonsoft.Json.Linq;
using System.Net;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, ExecutionContext context, TraceWriter log)
{
try
{
log.Info("C# ExceptionToInsightsAzure function processed a request.");
//Preparing the telemetry object
TelemetryClient telemetryClient = new TelemetryClient();
telemetryClient.InstrumentationKey = "APPINSIGHTS_INSTRUMENTATIONKEY";
telemetryClient.Context.Operation.Id = Guid.NewGuid().ToString();
dynamic data = await req.Content.ReadAsAsync<object>();
//We transform data to be able to query it
JObject dataToParse = JObject.Parse(Convert.ToString(data));
string inputParameter = Convert.ToString(dataToParse.SelectToken("InputParameters"));
var sharedVariablesData = dataToParse.SelectToken("SharedVariables");
// preparing the specific pieces of information we are looking for
string PluginAction = null;
string ExceptionMessage = null;
string EntityName = null;
string ExceptionStackTrace = null;
string sharedVariables = null;
Dictionary<string, string> sharedVariablesDic = new Dictionary<string, string>();
if (sharedVariablesData != null)
{
// Getting the keys info
foreach (var sharedVariable in sharedVariablesData){
sharedVariablesDic.Add(sharedVariable.SelectToken("key").ToString(), sharedVariable.SelectToken("value").ToString());
}
sharedVariables = Convert.ToString(sharedVariablesData);
PluginAction = sharedVariablesDic["PluginAction"];
log.Info($"PluginAction : {PluginAction}");
ExceptionMessage = sharedVariablesDic["ExceptionMessage"];
log.Info($"ExceptionMessage : {ExceptionMessage}");
ExceptionStackTrace = sharedVariablesDic["ExceptionStackTrace"];
log.Info($"ExceptionStackTrace : {ExceptionStackTrace}");
EntityName = sharedVariablesDic["EntityName"];
log.Info($"EntityName : {EntityName}");
}
var dataToString = Convert.ToString(data);
var evt = new EventTelemetry($"Exception in CRM plugin for entity {EntityName} ({PluginAction})");
var properties = new Dictionary<string, string>() {
{ "InputParameters", inputParameter },
{ "SharedVariables", sharedVariables },
{ "PluginAction", PluginAction },
{ "ExceptionMessage", ExceptionMessage },
{ "ExceptionStackTrace", ExceptionStackTrace },
{ "EntityName", EntityName }
};
telemetryClient.TrackEvent(evt.Name, properties);
}
catch (Exception ex)
{
log.Error($"Error : {ex.Message}");
return req.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
}
return req.CreateResponse(HttpStatusCode.OK, "Sent To Insights");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment