Azure应用程序洞察不显示在Azure函数中创建的自定义事件 [英] Azure Application Insights doesn't show custom events created in Azure function

查看:0
本文介绍了Azure应用程序洞察不显示在Azure函数中创建的自定义事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C#编写的函数应用程序,它是在Azure中创建的,由HttpTrigger触发。

我正在尝试记录几个自定义事件,以记录用于分析性能的有趣时间点。

我所做的是在构造函数中创建TelemetryClient。

  static ThumbnailGenerator()
    {
        telemetryClient = new TelemetryClient(TelemetryConfiguration.CreateDefault());
    }

然后在函数中:

    [FunctionName("Upload")]
    [StorageAccount("AzureWebJobsStorage")]
    public static async Task<IActionResult> Upload(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "upload/{name}")] HttpRequest req,
        string name,
        ILogger log,
        ExecutionContext context)
    {       
        var evt = new EventTelemetry("Upload function called");
        evt.Context.User.Id = name;
        telemetryClient.TrackEvent(evt);
        telemetryClient.Flush();

        ....

        DateTime start = DateTime.UtcNow;
    
        // Log a custom dependency in the dependencies table.
        var dependency = new DependencyTelemetry
        {
            Name = "Upload-Image-Operation",
            Timestamp = start,
            Duration = DateTime.UtcNow - start,
            Success = true
        };

        telemetryClient.TrackDependency(dependency);
        telemetryClient.Flush();

        return new OkObjectResult(name + "Uploaded successfully.");
    }

这方面的应用程序洞察可以始终正确地显示该函数的默认RequestTelemeter和跟踪。但是,自定义事件和DependencyTelemeter非常不稳定,有时显示在应用程序洞察中,有时根本不显示。

我做了很多研究,添加了如下内容:

    telemetryClient.Flush();

但不稳定性仍大致相同。

我使用的库函数是:

 <Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Azure.Storage.Blobs" Version="12.9.0" />
    <PackageReference Include="Microsoft.ApplicationInsights" Version="2.14.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.0.0-beta.2" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.25" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
    <PackageReference Include="SixLabors.ImageSharp" Version="1.0.3" />
    <PackageReference Include="System.Diagnostics.DiagnosticSource" Version="5.0.1" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

有人知道为什么吗?似乎Azure在这一部分真的很不稳定和有缺陷。请给我一些建议。

推荐答案

确保您的遥测配置设置了正确的检测密钥。我不确定TelemetryConfiguration.CreateDefault()是否获得正确的值。

此外,我还建议使用依赖项注入在构造函数中注入TelemetryClient。它已经开箱即用了。这样,您就不必自己创建实例,也不必担心如何正确设置插入键:

    [FunctionName("Upload")]
    [StorageAccount("AzureWebJobsStorage")]
    public static async Task<IActionResult> Upload(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "upload/{name}")] HttpRequest req,
        string name,
        ILogger log,
        TelemetryClient telemetryClient, 
        ExecutionContext context)
    {       
        telemetryClient.XXX();
        ...
    }

应该不需要刷新客户端,只有在终止时才需要刷新,即使这样,您也应该等待一段时间,因为这是一个异步进程。

这篇关于Azure应用程序洞察不显示在Azure函数中创建的自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆