【问题标题】:Azure function is not logging custom events, dependencies to app insightsAzure 函数未记录自定义事件、对应用洞察的依赖关系
【发布时间】:2021-02-10 02:18:54
【问题描述】:

我们在C# 中开发了一个Azure Function V3。我们尝试将一些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;
        }
}

【问题讨论】:

    标签: c# azure-functions azure-application-insights telemetry ilogger


    【解决方案1】:

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

    Microsoft.Azure.WebJobs.Logging.ApplicationInsights, version 3.0.18

    更新包Microsoft.NET.Sdk.Functions最新版本3.0.9。

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

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

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

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

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

    【讨论】:

    • 感谢您的建议。其他一切都很好,但我使用的是 Microsoft.ApplicationInsights 包而不是 Microsoft.Azure.WebJobs.Logging.ApplicationInsights。但我在 Microsoft.Azure.WebJobs.Logging.ApplicationInsights 库中看不到 TelemetryClient 对象。我错过了什么吗?
    • @Raj,TelemetryClientMicrosoft.ApplicationInsights 包中。但是当您安装包Microsoft.Azure.WebJobs.Logging.ApplicationInsights 时,也会自动安装包Microsoft.ApplicationInsights。因为这2个包有依赖关系。所以你也可以使用TelemetryClient
    • 我有所有这些包。但我仍然无法在应用洞察中看到日志。
    • @Raj,你在哪里运行这个项目?在天蓝色或本地?
    • @Raj,我建议您可以按照我回答中的步骤在本地进行测试。在测试过程中,请查看Visual Studio 输出窗口,看看是否有任何遥测数据正在发送。
    猜你喜欢
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    相关资源
    最近更新 更多