简体   繁体   中英

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. I had deployed my code to Azure Functions resource. I am able to see logs in Application Insights but when trying to see LIVE TELEMETRIC, data is not appearing. Its showing as "Not available: your app is offline or using an older SDK"

NuGet Package:

Microsoft.Extensions.Logging.ApplicationInsights

Below is my Code:

host.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:

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:

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:

在此处输入图像描述

What am I missing still?

An azure function is offline unless it gets triggered by a (in your case) HTTP call. 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.

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: 在此处输入图像描述

在此处输入图像描述

Then rebuild the code in VS and publish to Azure again. In the Azure Portal you can see the Live metrics as shown in below image if your Function App hosts successfully:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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