簡體   English   中英

Azure 函數未記錄自定義事件、應用洞察的依賴項

[英]Azure function is not logging custom events, dependencies to app insights

我們有一個用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;
        }
}

請確保您使用的是正確的軟件包,如下所示:

Microsoft.Azure.WebJobs.Logging.ApplicationInsights,版本 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 功能配置應用程序見解。

然后我測試了您的代碼,自定義事件或依賴項已正確登錄到應用程序洞察中。 這是屏幕截圖:

在此處輸入圖片說明

如果您仍有問題,請告訴我(並請提供更多詳細信息)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM