如何解决Azure功能未记录自定义事件,对应用程序见解的依赖性
我们有一个Azure Function V3
开发的C#
。我们尝试将一些custom events,dependencies
等记录到Azure Application Insights
。我们无法使用TelemetryClient
登录应用程序见解。代码运行正常,没有任何错误。另外,可以看到instrumentation key
从配置文件中检索到。但是,Ilogger
日志可以在应用程序洞察力跟踪表中找到。请在下面找到我们使用的代码,
public class CommunityCreate
{
private readonly TelemetryClient telemetryClient;
public CommunityCreate(TelemetryConfiguration telemetryConfiguration)
{
this.telemetryClient = new TelemetryClient(telemetryConfiguration);
}
[FunctionName("Function1")]
[return: ServiceBus("sample",Connection = "ServiceBusProducerConnection")]
public async Task<string> Run(
[HttpTrigger(AuthorizationLevel.Anonymous,"get","post",Route = "Account/{id:int?}")] HttpRequest req,string id,ILogger log)
{
//log.LogInformation("C# HTTP trigger function processed a request.");
DateTime start = DateTime.UtcNow;
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
var evt = new EventTelemetry("Function called");
evt.Context.User.Id = name;
this.telemetryClient.TrackEvent(evt);
// Generate a custom metric,in this case let's use ContentLength.
this.telemetryClient.GetMetric("contentLength").TrackValue(req.ContentLength);
// Log a custom dependency in the dependencies table.
var dependency = new DependencyTelemetry
{
Name = "GET api/planets/1/",Target = "swapi.co",Data = "https://swapi.co/api/planets/1/",Timestamp = start,Duration = DateTime.UtcNow - start,Success = true
};
dependency.Context.User.Id = name;
this.telemetryClient.TrackDependency(dependency);
telemetryClient.TrackEvent("Ack123 Recieved");
telemetryClient.TrackMetric("Test Metric",DateTime.Now.Millisecond);
return name;
}
}
解决方法
请确保您使用的是以下正确的软件包:
Microsoft.Azure.WebJobs.Logging.ApplicationInsights,version 3.0.18
和
将软件包Microsoft.NET.Sdk.Functions
更新为最新版本3.0.9。
如果您是在本地运行项目,请在APPINSIGHTS_INSTRUMENTATIONKEY
中添加local.settings.json
,如下所示:
{
"IsEncrypted": false,"Values": {
"AzureWebJobsStorage": "xxxx","FUNCTIONS_WORKER_RUNTIME": "dotnet","APPINSIGHTS_INSTRUMENTATIONKEY": "xxx"
}
}
或者,如果您正在azure门户上运行它,请使用azure功能配置“应用程序”见解。
然后,我测试了您的代码,自定义事件或依赖项已正确登录到应用程序见解中。这是屏幕截图:
如果您仍然遇到问题,请告诉我(并请提供更多详细信息)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。