c# - 使用 BlobTrigger 的 Azure 函数中的 Activity.Current 为 null

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

我有一个使用 BlobTrigger 绑定(bind)并用 C# 编写的 Azure 函数 (V3)。

为了使用 Application Insights RequestTelemetry 添加自定义属性

        Activity.Current?.AddTag("TraceId", traceId);

我需要根据此 Stackoverflow answer 的建议访问 Activity.Current 。但是,由于 Activity.Current 为 NULL,它不起作用。

我的包配置如下所示:

<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.Functions.Extensions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.0.0-beta.2" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.14" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.8" />
    <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>

我的函数如下所示:

        [FunctionName("Create-Thumbnail")]
        public async Task CreateThumbnail([BlobTrigger("input/{name}",  Source = BlobTriggerSource.EventGrid, Connection = "AzureWebJobsStorage")] Stream image,
            IDictionary<string,string> metadata,
            string name,
            ILogger log,
            ExecutionContext context)
        {         
            Activity.Current?.AddTag("TraceId", traceId);
        }

我研究了一整天,但没有找到任何解决方案。有谁知道可能是什么问题吗?

最佳答案

目前,它可以在 HTTP 触发器功能中工作,而这些功能最终出现在应用程序洞察中的请求的自定义属性中,但不再有效。请参阅here

GitHub 中提供了相同的 Activity.Current 值为 null 问题 Azure Function host & Application Insights

Application Insights .NET SDK 使用 DiagnosticSource (DiagnosticSourceUsersGuide)事件 (ActivityUserGuide)收集和关联遥测数据的步骤。

请在 Functions 存储库中提出问题 link here .

关于c# - 使用 BlobTrigger 的 Azure 函数中的 Activity.Current 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68867641/

相关文章:

c# - Cast<T> 的实现不是针对 Linq 的不可变风格吗?

json - 使用 KQL 和外部 data() 运算符从 json 文件中提取信息

performance - Azure 流分析太慢 - 时间值也无关紧要

azure - 如何解决 ARM 模板中的 'resource is not defined in the template.' 错误?

azure - KeyVault 防火墙配置和 Azure Functions 消耗计划

azure - Terraform 删除应用程序服务计划时出错 StatusCode=409 服务器场 [asp] 无法删除,因为它分配了 Web 应用程序 [azure-function]

c# - 如何启用对C :\home\site\wwwroot?中Azure函数使用的目录的读写访问

c# - 用于深度属性比较的表达式生成器

c# - Array.Copy 是否保证基于每个元素的原子读写?

c# - 系统.MissingMethodException : Method 'Android.Support.V4.Widget.DrawerLayout.AddDrawerListener' not found