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

Issue

This Content is from Stack Overflow. Question asked by PavanKumar GVVS

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.

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);
        }
    }
}

What am I missing still?



Solution

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.


This Question was asked in StackOverflow by PavanKumar GVVS and Answered by klekmek It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?