2016-01-27 170 views
3

我在應用程序中爲Application Insights編寫了自定義記錄器。在Azure門戶中查看App Insights時,我沒有看到任何異常或任何事件。這裏是記錄器類的代碼,當我調試代碼時,我看到一個分配給InstrumentationKey屬性的鍵,有什麼想法我在這裏做錯了?我是否需要將其他信息附加到客戶端或配置?應用程序洞察 - 記錄異常

public class AppInsightsLogger:ILogger 
{ 
    private TelemetryClient ai; 

    public AppInsightsLogger() 
    { 
     ai = new TelemetryClient(); 
     if (string.IsNullOrEmpty(ai.InstrumentationKey)) 
     { 
      // attempt to load instrumentation key from app settings 
      var appSettingsTiKey = AppSettings.InsightsKey; 
      if (!string.IsNullOrEmpty(appSettingsTiKey)) 
      { 
       TelemetryConfiguration.Active.InstrumentationKey = appSettingsTiKey; 
       ai.InstrumentationKey = appSettingsTiKey; 
      } 
      else 
      { 
       throw new Exception("Could not find instrumentation key for Application Insights"); 
      } 
     } 
    } 
    public void LogException(Exception ex) 
    { 
     ai.TrackException(ex); 
    } 
} 

回答

6

我創建了一個新的控制檯應用程序,安裝最新的穩定ApplicationInsights SDK和幾乎保持你的榜樣,有輕微但重要的差異 - 我要麼讓它調用TrackException後在關閉之前等待或添加TelemetryClient.Flush()

namespace logtest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      AppInsightsLogger logger = new AppInsightsLogger(); 
      logger.LogException(new InvalidOperationException("Is data showing?")); 

      // either wait for a couple of minutes for the batch to be sent of add ai.Flush() after ai.TrackException() to send the batch immediately 
      Console.ReadLine(); 
     } 
    } 

    public class AppInsightsLogger 
    { 
     private TelemetryClient ai; 

     public AppInsightsLogger() 
     { 
      ai = new TelemetryClient(); 
      if (string.IsNullOrEmpty(ai.InstrumentationKey)) 
      { 
       // attempt to load instrumentation key from app settings 
       var appSettingsTiKey = "<ikey>"; 
       if (!string.IsNullOrEmpty(appSettingsTiKey)) 
       { 
        TelemetryConfiguration.Active.InstrumentationKey = appSettingsTiKey; 
        ai.InstrumentationKey = appSettingsTiKey; 
       } 
       else 
       { 
        throw new Exception("Could not find instrumentation key for Application Insights"); 
       } 
      } 
     } 
     public void LogException(Exception ex) 
     { 
      ai.TrackException(ex); 
      // ai.Flush(); 
     } 
    } 
} 

首先,我可以看到遙測項目發送在Visual Studio調試輸出窗口: enter image description here

然後我看到遙測離開我的機器在Fidd我也可以看到它已被我們的數據收集終端成功接受。 enter image description here

最後,我可以看到它在門戶網站:

enter image description here