Azure 函数不记录自定义事件、对应用程序见解的依赖关系

问题描述 投票:0回答: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 Received");
        telemetryClient.TrackMetric("Test Metric", DateTime.Now.Millisecond);

        return name;
    }
}
c# azure-functions azure-application-insights telemetry ilogger
1个回答
1
投票

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.