[英]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.