如何解决.net core 3.1在http触发功能中添加遥测
我通过添加包 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”类型的服务。
解决方法
请遵循此 tutorial 并改用 Microsoft.Azure.WebJobs.Logging.ApplicationInsights
。这是 official document 推荐的。这是我的测试代码(只需在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"
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。