1.The ItemId can be fetched by using Application Insights REST API, but it has some time(around a few minutes) delay in runtime. The code is in the following.
2.Without using ItemId, you can use the operation_id, in your code, just use this line of code return System.Diagnostics.Activity.Current.RootId
, here is reference about operation_id
3.Example for using APP Insights REST API:
3.1 Getting your API key and Application ID, details here:
Nav to azure portal -> go to your application insights -> under configure
section, select API Access
-> Write down the application id, then click Create API Key, screenshot below, remember write down the key:

3.2 Construct the api url, like this:
https://api.applicationinsights.io/v1/apps/your_application_id/query?,
then construct your query string:
query=exceptions | where timestamp >= datetime("UTC time") | where operation_Id == "operation_id"
you can modify the query as you need.
3.3 My code as following, it returns json string, you can parse json to get ItemId:
public string MakeException()
{
try
{
int zipcode = int.Parse("123er");
}
catch (Exception ex)
{
DateTime dt = DateTime.UtcNow;
string timeFormat = dt.ToString("s");
telemetry.TrackException(ex);
telemetry.Flush();
System.Threading.Thread.Sleep(TimeSpan.FromMinutes(5)); // in runtime, need to wait for the result populated
string operation_id = System.Diagnostics.Activity.Current.RootId; //get the operation_id, will use it in the query string
string api = "https://api.applicationinsights.io/v1/apps/your_application_id/query?";
string query = @"query=exceptions | where timestamp >= datetime(""" + timeFormat + @""")";
query += " | where operation_Id == " + "\"" + operation_id + "\"";
api += query;
string apikey = "your api key";
//call the REST API
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("x-api-key", apikey);
var req = string.Format(api);
HttpResponseMessage response = client.GetAsync(req).Result;
if (response.IsSuccessStatusCode)
{
return response.Content.ReadAsStringAsync().Result; // return a json string, you can parse it to get the ItemId
}
else
{
return response.ReasonPhrase;
}
}
return "ok";
}