2017-06-04 53 views
1

其實,我在我的asp.net網站API 2設置應用程序的見解,記錄錯誤以下步驟Diagnose exceptions in your web apps with Application Insights,一切工作正常,但是,我想補充的信息對每個例外,對於例如CustomerId,JobId和其他。應用洞察 - 例外添加信息

所以我想看到異常(無論是調用堆棧或其他財產)通過Azure應用程序洞察(see this image)數據。這些信息可以幫助我檢測我應該用來嘗試複製錯誤場景的記錄。

你能告訴我任何建議嗎?

謝謝!

+3

閱讀有關'TelemetryInitializers',看到的是https://文檔.microsoft.com/EN-US /天藍色/應用程序的見解/ APP-見解-API過濾採樣#附加屬性 - itelemetryinitializer。您可以將自定義屬性添加到所有項目中,例如請求和異常等等。儘管如此,它仍由您來注入適當的值。你可以創建一個工廠類來完成JobId等工作。這取決於你如何訪問這些信息。 –

回答

4

據我所知,TelemetryClient.TrackException可以添加自定義屬性。

添加這些屬性之後,你會發現在蔚藍的見解門戶值。

更多有關如何在網頁API添加自定義屬性的詳細信息,你可以參考下面的代碼:

enter image description here

public class AiExceptionLogger : ExceptionLogger 
     { 
      public override void Log(ExceptionLoggerContext context) 
      { 
       if (context != null && context.Exception != null) 
       {//or reuse instance (recommended!). see note above 
        var ai = new TelemetryClient(); 
        //Here you need get the CustomerId, JobId, and other. For example get the current user id 
        string re = HttpContext.Current.User.Identity.Name; 

        // Set up some properties: 
        //Here you need get what you need and add them to the properties 
        var properties = new Dictionary<string, string> {{ "Users", "vvvvv" } }; 
         // Send the exception telemetry: 
        ai.TrackException(context.Exception, properties); 
       } 
       base.Log(context); 
      } 
     } 

你可以在見下面的所有屬性中找到它


但我有其他的問題,我該怎麼做才能從我的控制器「AiExceptio發送數據nLogger」。即:我有我的控制器POST方法Post(user,jobId),我想添加jobId TrackException。注意:我不想爲我的控制器中的每個方法使用try {} catch(){},如果我可以將該信息添加到上下文中,它將是grat !.謝謝!

根據您的描述,我建議您可以嘗試另一種方式註冊過濾器並重寫OnActionExecuted方法。

在這種方法中,你可以先檢查異常爲空。如果異常不爲null,則可以從HttpActionExecutedContext獲取ActionArguments。

然後,你可以在屬性中添加此參數,並將其發送到Azure應用程序見解。

更多細節,你可以參考下面的代碼:

WebApiConfig:

public static void Register(HttpConfiguration config) 
     { 
      // Web API configuration and services 

      // Web API routes 
      config.MapHttpAttributeRoutes(); 

      config.Routes.MapHttpRoute(
       name: "DefaultApi", 
       routeTemplate: "api/{controller}/{id}", 
       defaults: new { id = RouteParameter.Optional } 
      ); 

      //config.Services.Add(typeof(IExceptionLogger), new AiExceptionLogger()); 

      config.Filters.Add(new AAA()); 
     } 

     public class AAA : ActionFilterAttribute 
     { 

      public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) 
      { 
       if (actionExecutedContext.Exception != null) 
       { 
        var ai = new TelemetryClient(); 
        //here get the arguments 
        string d1 = (string)actionExecutedContext.ActionContext.ActionArguments["id"]; 
        var properties = new Dictionary<string, string> { { "Users", d1 } }; 
        ai.TrackException(actionExecutedContext.Exception, properties); 
       } 
       base.OnActionExecuted(actionExecutedContext); 
      } 
     } 

結果:

enter image description hereenter image description here

+0

非常感謝您的回答!但是我還有其他問題,我該如何將數據從我的控制器發送到「AiExceptionLogger」。一世。e:我的控制器有一個POST方法'Post(user,jobId)',我想將jobId添加到'TrackException'。注意:我不想爲我的控制器中的每個方法使用'try {} catch(){}',如果我可以將這些信息添加到上下文中,它將是grat !.謝謝! –

+0

我已經更新了答案。 –

+0

有沒有更新?如果您覺得我的回答很有用/有幫助,請將其標記爲答案,以便其他人可以從中受益。 –