Custom Application Insight TelemetryInitializer using Azure Function v2

徘徊边缘 提交于 2019-12-22 18:27:33

问题


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!

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.

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
        }
    }
}

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>();
        }
    }
}

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?

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)?

What are we missing?


回答1:


You are not missing anything and it should just work. There is a known issue that breaks WebJobStartup in functions.

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?



来源:https://stackoverflow.com/questions/54416463/custom-application-insight-telemetryinitializer-using-azure-function-v2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!