0

I have added the telemetry in Http trigger function by adding package Microsoft.ApplicationInsights" Version="2.17.0" to view the logs in application insight.

private readonly TelemetryClient _telemetry;

    public GoogleAuth(ShoppingContentService service, int maxListPageSize,TelemetryConfiguration telemetryConfiguration)
    {
        this.service = service;
        this.maxListPageSize = maxListPageSize;

        this._telemetry = new TelemetryClient(telemetryConfiguration);
    }

and I am using this telemetry inside my http trigger function .

_telemetry.TrackTrace($"[GoogleProductData]: Request body:{data}");

But I am getting this error.

An unhandled host error has occurred. [2021-06-17T13:08:55.752Z] Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type 'Google.Apis.ShoppingContent.v2_1.ShoppingContentService' while attempting to activate 'ShoppingSamples.Content.GoogleAuth'.

shashank shekhar
  • 132
  • 2
  • 15
  • Is the error you have mentioned is started appearing when you added the application insights package? if no (and I believe it should not be); then update the title and description accordingly. Based on error, it seems that error is due to unable to resolve `ShoppingContentService` instance. Can you check if the `ShoppingContentService` is registered in the service collection? – user1672994 Jun 17 '21 at 14:49
  • yes when i added telemetryConfiguration in constructor and using telemtery for logs then this error is coming. I am not using startup class as this is http trigger function which starts with run method. – shashank shekhar Jun 17 '21 at 18:26
  • You can see more info in your newly created question, and feel free to let me know if you got any further issue on application insights : ) – Tiny Wang Jun 25 '21 at 08:30

1 Answers1

0

Pls follow this tutorial and using Microsoft.Azure.WebJobs.Logging.ApplicationInsights instead. This is recommended by official document. This is my testing code( just create a new http trigger function in visual studio)

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;
using Microsoft.ApplicationInsights.Extensibility;

namespace FunctionApp1
{
    public class Function1
    {
        private readonly TelemetryClient telemetryClient;

        /// Using dependency injection will guarantee that you use the same configuration for telemetry collected automatically and manually.
        public Function1(TelemetryConfiguration telemetryConfiguration)
        {
            this.telemetryClient = new TelemetryClient(telemetryConfiguration);
        }

        [FunctionName("Function1")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

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

and adding APPINSIGHTS_INSTRUMENTATIONKEY to local.settings.json

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "APPINSIGHTS_INSTRUMENTATIONKEY": "instrument_key_here"
  }
}
Tiny Wang
  • 10,423
  • 1
  • 11
  • 29