Azure Function v4 无法登录应用程序洞察

问题描述 投票:0回答:1

我遇到的问题是,我的 Azure 函数的 Application Insights 中没有显示任何条目。 APPINSIGHTS_INSTRUMENTATIONKEYAPPLICATIONINSIGHTS_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();
    }

您对进一步审查的领域有什么建议吗?谢谢你。

c# azure function azure-application-insights
1个回答
1
投票

我也在本地尝试过,但在Azure环境中无法运行。使用.net 6.

我已在我的环境中重现,以下是预期结果:

首先在门户中断开本地和连接的应用程序洞察,如下所示:

enter image description here

然后连接如下:

enter image description here

单击“下一步”完成步骤(在接下来的步骤中选择您的应用程序洞察资源)

然后你会看到这样的东西:

enter image description here

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:

enter image description here

In portal:

enter image description here

遥测客户端输出:

尝试按照上述步骤获得所需的结果,就像我得到的那样。您也可以部署到Azure并运行它,然后您也会得到(不要忘记在环境变量部分添加连接字符串)。

© www.soinside.com 2019 - 2024. All rights reserved.