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.Log@R_8_4045@ion("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功能配置“应用程序”见解。

然后,我测试了您的代码,自定义事件或依赖项已正确登录到应用程序见解中。这是屏幕截图:

enter image description here

如果您仍然遇到问题,请告诉我(并请提供更多详细信息)。