c# - Azure Application Insights 不显示在 Azure 函数中创建的自定义事件

标签 c# azure azure-functions azure-application-insights

我有一个在 Azure 中创建的用 C# 编写的函数应用程序,由 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.");
    }

对此的应用程序见解可以始终正确显示默认的 RequestTelemetry 和该函数的跟踪。但是,自定义事件和 DependencyTelemetry 非常不稳定,有时在应用程序洞察中显示,有时根本不显示。

enter image description here

我做了很多研究并添加了类似的内容:

    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 在这部分确实不稳定且有 bug。请给我一些提示。

最佳答案

确保您的遥测配置具有正确的仪器 key 集。我不确定 TelemetryConfiguration.CreateDefault() 是否获得了正确的值。

此外,我建议使用依赖注入(inject)在构造函数中注入(inject) TelemetryClient。它已经开箱即用。这样您就不必自己创建实例,也不必担心设置检测 key 的正确方法:

    [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();
        ...
    }

刷新客户端不是必需的,仅在终止时才需要,即使如此,您也应该等待一段时间,因为它是一个异步进程。

关于c# - Azure Application Insights 不显示在 Azure 函数中创建的自定义事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68848609/

相关文章:

c# - 查询包含对在不同数据上下文中定义的项目的引用

c# - 如何将项目(文本和值)添加到 ComboBox 并在 SelectedIndexChanged(SelectedValue = null)中读取它们

c# - 无法发布 Azure 函数

azure - 在 VS code 上添加参数文件以进行 Bicep 模板部署

Azure Functions - 应用程序见解 - 自定义遥测 - EventSource 实例已存在

azure - 比较 AWS-lambda、Azure 函数和 Google Cloud Function

c# - 向外部方发出出站调用时,Azure Functions 中没有静态 IP 地址

c# - 是否有异常错误类值列表及其含义?具体是sqlexception

azure - Microsoft Azure AKS 位置(命令行字符串)

c# - 从内存位置复制字节时是否需要固定结构