How to link exceptions to requests in Application Insights on Azure?

前端 未结 4 591
没有蜡笔的小新
没有蜡笔的小新 2020-12-23 23:46

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:



        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-24 00:39

    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());
    

提交回复
热议问题