使用Azure Function v2的自定义Application Insight TelemetryInitializer [英] Custom Application Insight TelemetryInitializer using Azure Function v2
问题描述
在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类-这似乎是一个已知的
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
方法却不会触发(在Core 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)?
我们缺少什么?
推荐答案
您不会丢失任何东西,它应该可以正常工作.有一个已知问题破坏了WebJobStartup函数.
You are not missing anything and it should just work. There is a known issue that breaks WebJobStartup in functions.
EventTelemtery(它们报告请求,依赖项,异常,跟踪和度量标准).您可以检查Net Core 2.0上是否用其他类型的遥测调用了initialize吗?
EventTelemtery is never reported by the SDK automatically (they report Requests, Dependencies, Exceptions, Traces and Metrics). Can you check if initialize is called with other types of telemetry on net core 2.0?
这篇关于使用Azure Function v2的自定义Application Insight TelemetryInitializer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!