c# - 在 .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”类型的服务。
解决方案
请遵循本教程并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"
}
}
推荐阅读
- scala - UDF Scala 火花语法
- python - 下载文件时的Python多处理比正常实现慢
- c++ - 修改 std::valarray 的元素是否安全
同时? - ios - 基于屏幕(超级视图)高度的 iOS 顶部对齐约束
- c# - recaptcha 上的错误禁止 403
- c# - 在对话框的构造函数中调用 .Show() 或 .ShowDialog 是一种好习惯吗
- liferay - Liferay Service Pack 和 Fixpack
- mysql - 错误 1054 (42S22):“字段列表”中的未知列“i”
- scala - 如何在 scala 中使用 google pubsub 库
- apache-spark - 在火花流中随机获取 LeaseExpiredException