1

我们有一个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;
        }
}
4

1 回答 1

1

请确保您使用的是正确的软件包,如下所示:

Microsoft.Azure.WebJobs.Logging.ApplicationInsights,版本 3.0.18

将软件包更新Microsoft.NET.Sdk.Functions到最新版本 3.0.9。

如果您在本地运行项目,请添加APPINSIGHTS_INSTRUMENTATIONKEYin local.settings.json,如下所示:

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "xxxx",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "APPINSIGHTS_INSTRUMENTATIONKEY": "xxx"
  }
}

或者,如果您在 azure 门户上运行它,请使用 azure 功能配置应用程序洞察力。

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

在此处输入图像描述

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

于 2020-10-28T09:36:13.213 回答