简体   繁体   English

实时遥测不适用于 Azure Function .NET Core 6

[英]Live Telemetric is not working for Azure Function .NET Core 6

I created a basic Azure Function (HTTP Trigger) using .NET Core6 and integrated Application Insights,enabled Live Telemetric.我使用 .NET Core6 创建了一个基本的 Azure Function(HTTP 触发器)并集成了 Application Insights,启用了实时遥测。 I had deployed my code to Azure Functions resource.我已将我的代码部署到 Azure Functions 资源。 I am able to see logs in Application Insights but when trying to see LIVE TELEMETRIC, data is not appearing.我能够在 Application Insights 中看到日志,但在尝试查看 LIVE TELEMETRIC 时,没有显示数据。 Its showing as "Not available: your app is offline or using an older SDK"它显示为“不可用:您的应用处于离线状态或使用较旧的 SDK”

NuGet Package: NuGet Package:

Microsoft.Extensions.Logging.ApplicationInsights Microsoft.Extensions.Logging.ApplicationInsights

Below is my Code:以下是我的代码:

host.json主机.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"
    }
  }
}

startup.cs:启动.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;
        }
    }
}

Local Settings:本地设置:

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "APPINSIGHTS_INSTRUMENTATIONKEY": "<<KEY>>",
    "APPLICATIONINSIGHTS_CONNECTION_STRING": "<<STRING>>"
  }
}

My Function Code:我的 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);
        }
    }
}

Application Insights Logs in Azure Portal: Azure 门户中的 Application Insights 日志:

在此处输入图像描述

What am I missing still?我还缺少什么?

An azure function is offline unless it gets triggered by a (in your case) HTTP call. azure function 处于离线状态,除非它被(在您的情况下)HTTP 调用触发。 That's why the "Live Metrics" give you that error.这就是“实时指标”给你这个错误的原因。 If you really need Live Metrics, you could consider changing your Function to a Durable Function, but that has other implications.如果您真的需要 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: 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:

在此处输入图像描述

You have to create an Azure Application Insights in azure portal and copy the Instrumentation key of that resource and paste it in the localsettings.json' file as shown in below image:您必须在 azure 门户中创建 Azure Application Insights 并复制该资源的Instrumentation key并将其粘贴到localsettings.json'文件中,如下图所示: 在此处输入图像描述

在此处输入图像描述

Then rebuild the code in VS and publish to Azure again.然后在VS中重建代码,再次发布到Azure。 In the Azure Portal you can see the Live metrics as shown in below image if your Function App hosts successfully:在 Azure 门户中,如果您的 Function 应用程序成功托管,您可以看到如下图所示的实时指标:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM