We are using Owin on Azure for a REST service, and have to report to Application Insights directly. We want to log exceptions and requests. Right now we have this:
There is an overload of TelemetryClient.TrackException method that accepts dictionary of properties. It is designed specifically to clasify and search for the exception. This allows to generate an error Id, and link the error to AppInsights.
An example of error handling:
var errorId = GenerateErrorId();
var trackProperties = new Dictionary();
trackProperties.Add("ErrorId", errorId);
var ai = new TelemetryClient();
ai.TrackException(exception, trackProperties);
JObject resp = new JObject();
resp["message"] = exception.Message + " - " + errorId;
await context.Response.WriteAsync(resp.ToString());