[英]Live Telemetric is not working for Azure Function .NET Core 6
我使用 .NET Core6 創建了一個基本的 Azure Function(HTTP 觸發器)並集成了 Application Insights,啟用了實時遙測。 我已將我的代碼部署到 Azure Functions 資源。 我能夠在 Application Insights 中看到日志,但在嘗試查看 LIVE TELEMETRIC 時,沒有顯示數據。 它顯示為“不可用:您的應用處於離線狀態或使用較舊的 SDK”
NuGet Package:
Microsoft.Extensions.Logging.ApplicationInsights
以下是我的代碼:
主機.json
{
"version": "2.0",
"logging": {
"fileLoggingMode": "always",
"applicationInsights": {
"enableLiveMetrics": true,
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
},
"logLevel": {
"default": "Information",
"Host": "Error",
"Function": "Error",
"Host.Aggregator": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"extensions": {
"http": {
"routePrefix": "api"
}
}
}
啟動.cs:
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Azure.WebJobs.Host.Bindings;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OSH_Function;
using System;
[assembly: FunctionsStartup(typeof(Startup))]
namespace Function1
{
public class Startup : FunctionsStartup
{
public IConfiguration configuration { get; set; }
public override void Configure(IFunctionsHostBuilder builder)
{
//Load App Settings
configuration = BuildConfiguration(builder.GetContext().ApplicationRootPath);
builder.Services.AddSingleton(new TelemetryConfiguration { ConnectionString = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING")});
}
private IConfiguration BuildConfiguration(string applicationRootPath)
{
var config =
new ConfigurationBuilder()
.SetBasePath(applicationRootPath)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddJsonFile("settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
return config;
}
}
}
本地設置:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"APPINSIGHTS_INSTRUMENTATIONKEY": "<<KEY>>",
"APPLICATIONINSIGHTS_CONNECTION_STRING": "<<STRING>>"
}
}
我的 Function 代碼:
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;
namespace FunctionApp1
{
public static class Function2
{
[FunctionName("Function2")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
log.LogTrace("ILogger: xxxxxxxxxxxxxxxxxxxxxxxxx");
log.LogDebug("ILogger: debug message from azure function");
log.LogInformation("ILogger: information message from azure function");
log.LogWarning("ILogger: warning message from azure function");
log.Log(LogLevel.Error, "ILogger: error message from azure function");
string name = req.Query["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.";
return new OkObjectResult(responseMessage);
}
}
}
Azure 門戶中的 Application Insights 日志:
我還缺少什么?
azure function 處於離線狀態,除非它被(在您的情況下)HTTP 調用觸發。 這就是“實時指標”給你這個錯誤的原因。 如果您真的需要 Live Metrics,可以考慮將 Function 更改為 Durable Function,但這還有其他含義。
I tried to reproduce the issue using the Visual Studio 2022 with .net core 6.0 and created the function app, published to Azure Function App successfully as shown below:
您必須在 azure 門戶中創建 Azure Application Insights 並復制該資源的Instrumentation key
並將其粘貼到localsettings.json'
文件中,如下圖所示:
然后在VS中重建代碼,再次發布到Azure。 在 Azure 門戶中,如果您的 Function 應用程序成功托管,您可以看到如下圖所示的實時指標:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.