0

我通过添加包 Microsoft.ApplicationInsights" Version="2.17.0" 在应用程序洞察力中查看日志,在 Http 触发功能中添加了遥测。

私有只读 TelemetryClient _telemetry;

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

        this._telemetry = new TelemetryClient(telemetryConfiguration);
    }

我在我的 http 触发函数中使用这个遥测。

_telemetry.TrackTrace($"[GoogleProductData]: 请求正文:{data}");

但我收到了这个错误。

发生了未处理的主机错误。[2021-06-17T13:08:55.752Z] Microsoft.Extensions.DependencyInjection.Abstractions:尝试激活“ShoppingSamples.Content.GoogleAuth”时,无法解析“Google.Apis.ShoppingContent.v2_1.ShoppingContentService”类型的服务。

4

1 回答 1

0

请遵循本教程Microsoft.Azure.WebJobs.Logging.ApplicationInsights改为使用。这是官方文档推荐的。这是我的测试代码(只需在 Visual Studio 中创建一个新的 http 触发函数)

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

并添加APPINSIGHTS_INSTRUMENTATIONKEY到 local.settings.json

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "APPINSIGHTS_INSTRUMENTATIONKEY": "instrument_key_here"
  }
}
于 2021-06-25T08:12:15.897 回答