问题描述
在 Azure 函数中,我们使用 .NET HttpClient 类、Azure Servicebus SDK 和 Azure Storage SDK 发出大量请求 - 所有内容都通过 Application Insight 日志记录中的构建精美地记录下来,显示依赖关系和所有内容!
In a Azure Function we make a number of request using the .NET HttpClient class, the Azure Servicebus SDK and the Azure Storage SDK - everything is beautifully logged via the build in Application Insight logging, showing dependencies and all!
现在我们想添加一个 ITelemetryInitializer
的实现,为上面提到的 SDK 编写的 EventTelemetry
添加一些维度.
Now we however like to add an implementation of ITelemetryInitializer
to add a few dimensions to the EventTelemetry
being written by the SDKs mentioned above.
我们首先创建一个实现 ITelemetryInitializer
接口的类.
We start by creating a class implementing the ITelemetryInitializer
interface.
public class ReferrerTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
if (telemetry is EventTelemetry)
{
// Add some code
}
}
}
然后我创建一个实现 IWebJobsStartup
接口的类,以 DI 注入我的初始化程序.
I then create a class implementing the IWebJobsStartup
interface to DI inject my initializer.
[assembly: WebJobsStartup(typeof(Startup))]
namespace LoggingTest
{
public class Startup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
builder.Services.AddSingleton<ITelemetryInitializer, ReferrerTelemetryInitializer>();
}
}
}
注意:我只在 .NET Core 2.0(而不是 2.1)中触发了 Startup 类 - 这似乎是一个已知的开放issue 虽然?
Note: I only got the Startup class to fire in .NET Core 2.0 (and not in 2.1) - this seems to be an open an known issue though?
但是,初始化程序类中的 Initialize
方法却永远不会触发(在核心 2.0 中,在我看到 Startup 类已注册 DI ReferrerTelemetryInitializer
实现之后)?
BUT, the Initialize
method in the initializer class does however never fires (in core 2.0, after I've seen that the Startup class has registered the DI ReferrerTelemetryInitializer
implementation)?
我们缺少什么?
推荐答案
Azure Function v3
对于那些在 2021 年在 Azure Function v3 中进行这项工作的人来说,这现在可以工作了.
For those that work on this in 2021 in Azure Function v3 this does work now.
TelemetryClient 由 Azure Function 主机/运行时配置.它将通过依赖注入从服务集合中获取注册的 ITelemetryInitializer.
The TelemetryClient is configured by the Azure Function host/runtime. It will pick up the registered ITelemetryInitializer via dependency injection from the service collection.
项目文件
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.27" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
启动
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<ITelemetryInitializer, TheCustomTelemetryInitializer>();
}
}
对本地发展很重要!
然后您需要在 local.settings.json 中为您的 APPINSIGHTS_INSTRUMENTATIONKEY 添加一些值.这是它与常规控制台应用程序不同的地方,即使检测密钥为空,它也可以在本地工作.在功能应用程序中,您显然需要在那里添加一些价值.它可以是任何东西,我已经做到了,而且效果很好:
Then you need to add some value to your APPINSIGHTS_INSTRUMENTATIONKEY in local.settings.json. This is where it differs from regular console apps, where even if the instrumentation key is empty it will work locally. In function apps apparently you need to add some value there. It can be anything, I've done this and it worked fine:
来源
"APPINSIGHTS_INSTRUMENTATIONKEY": "you-need-to-have-anything-at-all-here-so-it-will-work-locally"
这篇关于使用 Azure Function v2 自定义 Application Insight TelemetryInitializer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!