我正在尝试从RequestTelemetry Azure函数v3中增强AppInsights中的HttpTrigger。
函数使用DI和Startup类初始化。
[assembly: FunctionsStartup(typeof(Startup))]
namespace Hager.Example.FunctionApp.FunctionApp
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
// No service for repro
}
}
}和我的职能
public class Function1
{
private readonly ILogger _logger;
public Function1(ILogger<Function1> logger)
{
_logger = logger;
}
[FunctionName("HttpTriggered")]
public IActionResult Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req)
{
using var loggerScope = _logger.BeginScope("{InScope1}{InScope2}{InScope3}", Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid());
_logger.LogInformation("Started Execution");
_logger.LogWarning("With a custom property: {CustomProperty}.", Guid.NewGuid());
Activity.Current?.AddTag("TagStart", Guid.NewGuid());
if (Activity.Current == null)
{
// Always null
_logger.LogError("No ActivityCurrent {Activity}.", Activity.Current);
_logger.LogError("ActivityCurrent Tags {Activity}.", Activity.Current?.Tags);
}
// Activity.Current.AddTag("Tag2", Guid.NewGuid()); // <- NullException
_logger.LogInformation("Finished Execution");
return new NoContentResult();
}
}我的项目包:
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.17.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.17.0" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
</ItemGroup>每个记录器和作用域都按预期工作,但是对象ActivityTag总是空的,在Azure函数中不应该是空的。
我错过了什么吗?
发布于 2021-03-18 09:17:13
更新:
添加了Op的解决方案:通过使用request.HttpContext?.Features.Get<RequestTelemetry>(),它工作得很好。
请卸载2 Application Insights packages:Microsoft.ApplicationInsights和Microsoft.ApplicationInsights.AspNetCore。
默认情况下,Application包不收集活动标记。所以这应该是原因。
我在没有安装上述2 Application Insights packages的情况下测试了您的azure功能,它工作得很好。下面是测试结果的屏幕截图:

在这里添加我的local.settings.json供您参考,代码与您的代码相同:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "xxxx",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"APPINSIGHTS_INSTRUMENTATIONKEY": "xxx"
}
}https://stackoverflow.com/questions/66670543
复制相似问题