我遇到的问题是,我的 Azure 函数的 Application Insights 中没有显示任何条目。 APPINSIGHTS_INSTRUMENTATIONKEY 和 APPLICATIONINSIGHTS_CONNECTION_STRING 已根据 Application Insights 建立并正式审核,确认其准确性。 Microsoft.Azure.WebJobs.Logging.ApplicationInsights 已安装,它是 C# Azure Function v4。
下面的代码说明了该函数的结构。
public async Task<IActionResult> xxxx(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("triggered...)");
}
builder.Services.Configure<TelemetryConfiguration>(config =>
{
config.SetAzureTokenCredential(infraCredential);
});
builder.Services.AddApplicationInsightsTelemetry();
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
var context = builder.GetContext();
// using Key Vault, either local dev or deployed
builder.ConfigurationBuilder
.AddJsonFile(Path.Combine(context.ApplicationRootPath, "appsettings.json"), optional: true,
reloadOnChange: false)
.AddJsonFile(Path.Combine(context.ApplicationRootPath, $"appsettings.{context.EnvironmentName}.json"),
optional: true, reloadOnChange: false)
.AddJsonFile(Path.Combine(context.ApplicationRootPath, "local.settings.json"), optional: true,
reloadOnChange: false)
.AddEnvironmentVariables()
.Build();
}
您对进一步审查的领域有什么建议吗?谢谢你。
我也在本地尝试过,但在Azure环境中无法运行。使用.net 6.
我已在我的环境中重现,以下是预期结果:
首先在门户中断开本地和连接的应用程序洞察,如下所示:
然后连接如下:
单击“下一步”完成步骤(在接下来的步骤中选择您的应用程序洞察资源)
然后你会看到这样的东西:
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;
namespace FunctionApp81
{
public class Function1
{
private readonly TelemetryClient telcl;
public Function1(TelemetryClient teleyClient)
{
telcl = teleyClient;
}
[FunctionName("Function1")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("Hello Rithwik , I am From logger.");
telcl.TrackEvent("Hello Rithwik how are you from telemetry client");
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)
? "Hello Rithwik plz pass a name"
: $"Hello, {name}. ";
return new OkObjectResult(responseMessage);
}
}
}
local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"APPINSIGHTS_INSTRUMENTATIONKEY": "32260fba-8fd2-d146"
}
}
Output:
In portal:
遥测客户端输出:
尝试按照上述步骤获得所需的结果,就像我得到的那样。您也可以部署到Azure并运行它,然后您也会得到(不要忘记在环境变量部分添加连接字符串)。