《Windows Azure Platform 系列文章目錄》
本文介紹的是由世紀互聯運維的Azure China。
目前Azure China是要通過代碼來配置Application Insight監控的,本文會詳細介紹相關的內容。
1.首先,我們先創建1個新的資源組
2.然后在這個資源組里,創建新的Azure Function。如下圖:
3.創建這個Function的時候,還會默認創建Application Insight。如下圖:
4.創建完畢后,資源如下:
5.我們選擇上面已經創建的Azure Function,打開Application Insight功能
選擇完畢后,點擊Apply生效。
6.然后我們在本地PC,創建1個Azure Function項目:
7.新建一個Function,類型為Http Trigger
8.在項目目錄中,增加starup.cs類。如下圖:
9.startup.cs代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.ApplicationInsights.Channel; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.ApplicationInsights.Extensibility.Implementation.ApplicationId; using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse; using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel; using Microsoft.Azure.Functions.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection; [assembly: FunctionsStartup(typeof(FunctionApp1.Startup))] namespace FunctionApp1 { internal class Startup : FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { var quickPulseFactory = builder.Services.FirstOrDefault(sd => sd.ServiceType == typeof(ITelemetryModule) && sd.ImplementationType == typeof(QuickPulseTelemetryModule)); if (quickPulseFactory != null) { builder.Services.Remove(quickPulseFactory); } var appIdFactory = builder.Services.FirstOrDefault(sd => sd.ServiceType == typeof(IApplicationIdProvider)); if (appIdFactory != null) { builder.Services.Remove(appIdFactory); } var channelFactory = builder.Services.FirstOrDefault(sd => sd.ServiceType == typeof(ITelemetryChannel)); if (channelFactory != null) { builder.Services.Remove(channelFactory); } builder.Services.AddSingleton<ITelemetryModule, QuickPulseTelemetryModule>(_ => new QuickPulseTelemetryModule { QuickPulseServiceEndpoint = "https://live.applicationinsights.azure.cn/QuickPulseService.svc" }); builder.Services.AddSingleton<IApplicationIdProvider, ApplicationInsightsApplicationIdProvider>(_ => new ApplicationInsightsApplicationIdProvider() { ProfileQueryEndpoint = "https://dc.applicationinsights.azure.cn/api/profiles/{0}/appId" }); builder.Services.AddSingleton<Microsoft.ApplicationInsights.Channel.ITelemetryChannel>(_ => new ServerTelemetryChannel() { EndpointAddress = "https://dc.applicationinsights.azure.cn/v2/track" }); } } }
10.在項目中,增加對下面依賴性的引用:
11.在項目根目錄中,增加文件:appsettings.json
{ "ApplicationInsights": { "InstrumentationKey": "[Application Insight的秘鑰]" }, "Logging": { "LogLevel": { "Default": "None" } } }
12.上面的Application Insight的秘鑰,我們可以通過查看Application Insight的訪問秘鑰:
頁面跳轉:
把上面的Instrumentation Key,粘貼到步驟11的配置項里。
13.在Visual Studio項目中,修改Function1.cs的代碼:
using System; using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Extensions.Http; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights.Extensibility; namespace FunctionApp1 { public static class Function1 { [FunctionName("Function1")] public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { string name = req.Query["name"]; //增加的代碼 var telemetry = new TelemetryClient(); telemetry.TrackTrace("Request " + name); //代碼結束 string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); name = name ?? data?.name; string responseMessage = string.IsNullOrEmpty(name) ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response." : $"Hello, {name}. This HTTP triggered function executed successfully."; //增加的代碼 telemetry.TrackTrace("Execute finished"); //代碼結束 return new OkObjectResult(responseMessage); } } }
讀者注意上面的
//增加的代碼部分 和 //代碼結束 部分
主要是將服務器端的數據log進行輸出,輸出到Azure Application Insight
14.Azure Function代碼修改完畢后,我們就可以從Azure雲端,下載發布配置文件。
選擇下載發布配置文件。如下圖:
15.將上面的發布文件保存到本地,回到Visual Studio項目
16.把我們在步驟14中的下載的文件,導入配置文件,然后重新在本地Visual Studio發布應用。
17.發布完畢后,我們可以訪問Function進行調試。
18.如下圖,拿到Function的url
19.我們在瀏覽器里,多次請求上面的url。步驟略。
20.多次訪問url之后,我們可以在Azure Application Insight的Search,查看到我們在步驟13中,增加的代碼片段。