我有一个使用 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/