2017-08-04 124 views
1

我正在使用ILogger接口來記錄Azure函數中的事件。我可以在Azure中發佈它,並將它連接到Azure中的Application Insights。如何在Azure函數中使用Application Insights?

我想在開發過程中在我的Visual Studio中的Application Insights中查看我的日誌。在here中,我可以看到在ASP.NET核心Web應用程序中將這些代碼放入Startup.cs中是可能的。 Azure函數使用VS 2017中的工具中的新項目模板可能類似嗎?

我使用VS 2017和Azure Function CLI 1.0.0-beta-100。

+0

據我所知,如果你想記錄信息到Application Insights。我建議你可以考慮使用Microsoft.ApplicationInsights包並使用InstrumentationKey創建TelemetryClient。更多細節,你可以參考這篇文章(https://cmatskas.com/azure-functions-custom-logging-with-appinsights/)。 –

+1

引用的文章說本身已經過時,因爲Azure函數已經與Application Insights集成。但是,該集成在Visual Studio中無法使用,以查看VS中的日誌而不是門戶。 – gabomgp

回答

2

據我所知,目前我們無法直接在您的本地天藍色功能項目中包含Application Insights。

這是一種解決方法:

您需要自行實施。從Nuget包管理器安裝Microsoft.ApplicationInsights之後。

然後使用TelemetryClient將日誌發送到天藍色。

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

[FunctionName("HttpTriggerCSharp")] 
    public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log) 
    { 


     var appInsights = GetTelemetryClient(); 
     //track an event 
     appInsights.TrackEvent("I am starting now. I'm timer triggered"); 
     // track a numeric value 
     appInsights.TrackMetric("Ticks based on current time", DateTime.Now.Ticks); 
     // track an exception 
     appInsights.TrackException(new Exception($"Random exception at {DateTime.Now}")); 

     // send data to azure 
     appInsights.Flush(); 

     log.Info($"C# Timer trigger function executed at: {DateTime.Now}"); 


     log.Info("C# HTTP trigger function processed a request."); 

     // parse query parameter 
     string name = req.GetQueryNameValuePairs() 
      .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0) 
      .Value; 

     // Get request body 
     dynamic data = await req.Content.ReadAsAsync<object>(); 

     // Set name to query string or body data 
     name = name ?? data?.name; 

     return name == null 
      ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body") 
      : req.CreateResponse(HttpStatusCode.OK, "Hello " + name); 
    } 

    private static TelemetryClient GetTelemetryClient() 
    { 
     var telemetryClient = new TelemetryClient(); 
     telemetryClient.InstrumentationKey = "Your InstrumentationKey"; 
     return telemetryClient; 
    } 
} 

}

結果:

enter image description here

1

添加應用洞察(AI)遙測Azure的功能很簡單,直截了當。

您只需要這些two步驟從官方文檔;

  1. 創建Application Insights實例。
    • 應用程序類型應設置爲一般
    • 抓住儀器關鍵
  2. 更新您的功能應用的設置
    • 添加應用程序設置 - APPINSIGHTS_INSTRUMENTATIONKEY = {儀表重點}

然而,在讓它進入雲端之前,如何在本地開發函數應用程序的同時捕獲AI遙測數據並不那麼明顯。首先,您需要prepare Azure函數的本地開發環境。然後你幾乎準備好了。您只需要在local.settings.json文件中重複上述第二步。所以你的文件應該是這樣的;

{ 
    "IsEncrypted": false, 
    "Values": { 
    "FUNCTIONS_EXTENSION_VERSION": "beta", 
    "AzureWebJobsStorage": "UseDevelopmentStorage=true", 
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true", 
    "HOST_NAME": "localhost:7072", 
    "APPINSIGHTS_INSTRUMENTATIONKEY": "11111111-2222-3333-4444-555555555555" 
    }, 
    "Host": { 
    "LocalHttpPort": 7072 
    } 
} 

在我來說,我有2種AI情況下,APPINSIGHTS_INSTRUMENTATIONKEY值在我的Azure的門戶網站,在我的本地開發ENV(local.settings.json)是不同的,所以,我不會混淆PROD和DEV遙測數據。

相關問題