[英]How can I use Application Insights locally with Azure Functions?
我正在使用 ILogger 接口在我的 Azure 函數中記錄事件。 我可以在 Azure 中發布它,並將其連接到 Azure 中的 Application Insights。
我想在開發期間在 Visual Studio 的 Application Insights 中查看我的日志。 在這里,我可以看到在 ASP.NET 核心 Web 應用程序中將一些代碼放入 Startup.cs 中是可能的。 Azure 函數是否可以使用 VS 2017 中工具的新項目模板進行類似的操作?
我正在使用 VS 2017 和 Azure Function CLI 1.0.0-beta-100。
將應用程序洞察(AI)遙測添加到Azure功能簡單明了。
你只需要官方文檔中的這兩個步驟;
然而,顯而易見的是如何在本地開發功能應用程序時捕獲AI遙測數據,就在它讓它進入雲端之前。 首先,您需要為Azure Functions 准備本地開發環境。 那你幾乎准備好了。 您只需要在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實例,我的Azure門戶和我的本地開發環境( local.settings.json
)中的APPINSIGHTS_INSTRUMENTATIONKEY
值是不同的,所以我不會混淆prod和dev遙測。
據我所知,目前我們無法在您當地的azure功能項目中直接包含Application Insights。
這是一個解決方法:
你需要自己實現它。 從Nuget包管理器安裝了Microsoft.ApplicationInsights之后。
然后使用TelemetryClient將日志發送到azure。
更多細節,您可以參考以下代碼:
[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;
}
結果:
因此,顯然由於對庫進行了更改,它會不時地停止工作。 所以這就是幫助我在2019年4月開始工作的原因。希望它能幫到別人。
我沒有添加任何日志或任何特定於我的功能應用程序,現在對我來說,它幾乎將所有控制台數據打印到我的應用程序洞察中的跟蹤日志(本地,不需要在Azure中配置任何內容)。 此外,我的測試場景在TargetFramework netstandard2.0
和AzureFunctionsVersion 2
上運行。
首先,您需要將此NuGet包添加到功能應用程序: https : //www.nuget.org/packages/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/
然后你必須向項目根目錄添加一個空文件: ApplicationInsights.config
你不需要在構建或任何東西時將它復制到輸出,這只是必須存在的東西或Application Insights不能在本地工作。
然后你需要在local.settings.json
為你的APPINSIGHTS_INSTRUMENTATIONKEY
添加一些值。 這是它與常規控制台應用程序的不同之處,即使檢測鍵是空的,它也可以在本地工作。 在功能應用中顯然你需要在那里添加一些價值。 它可以是任何東西,我已經完成了它並且它工作正常:
"APPINSIGHTS_INSTRUMENTATIONKEY": "you-need-to-have-anything-at-all-here-so-it-will-work-locally"
這是一個適用於我的解決方案,在隔離模式下使用 Azure Function v4 .NET6。 在找到此解決方案之前,我嘗試了 host.json 中的許多設置組合,但似乎什么也沒做。 現在,我在 Application Insights 中正確跟蹤實時指標、事務搜索和應用程序 Map 中的 Entity Framework Core 依賴項(以及 SignalR)。
來自本地調試 session 的本地數據將顯示在 Azure(實時和歷史遙測)內的 Application Insights 門戶和 Visual Studio 2022 的 Application Insights 搜索遙測中。 請注意,本地數據需要幾分鍾才能開始填充到搜索遙測功能中。 如果您沒有找到本地調試數據,請記住 Visual Studio 必須在調試模式下運行 function,並且您可能還需要連接到在線 Application Insights 服務(而不是調試 session 遙測):
嘗試將調試 session 遙測更改為 Azure 中的 Application Insights 在線實例。 點擊“Debug session telemetry”更改數據來源。
作為參考,這個 github 問題“ 在 v4 和 .Net 6 #760 中使用 Application Insights 的正確方法”導致遇到了這個解決方案。
添加 package:
Install-Package Microsoft.Data.SqlClient -Version 5.0.0
然后在 program.cs 中添加:
services.AddApplicationInsightsTelemetryWorkerService();
作為參考,一個工作主機。json:
{
"version": "2.0",
"logging": {
"fileLoggingMode": "always",
"logLevel": {
"default": "Information",
"Host": "Warning",
"Microsoft": "Warning"
},
"applicationInsights": {
"samplingSettings": {
"maxTelemetryItemsPerSecond": 5,
"isEnabled": true,
"excludedTypes": "Request;Exception"
},
"enableLiveMetrics": true,
"enableDependencyTracking": true,
"DependencyTrackingOptions": { "enableSqlCommandTextInstrumentation": true },
"enablePerformanceCountersCollection": true
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.